UIViewController旋转问题 [英] UIViewController rotating problem

查看:37
本文介绍了UIViewController旋转问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有不同ViewController的TabbarController.其中一些ViewController应该自动旋转,而一个ViewController应该仅在PORTRAIT中出现.我用以下过程填充Tabbar:

I have a TabbarController with different ViewControllers. Some of these ViewControllers should autorotate and one ViewController should be presented only in PORTRAIT. I fill the Tabbar with the following procedure:

ChartThemeViewController *chartViewController = [[ChartThemeViewController alloc] 
                                                           initWithTheme:chartThemeDict];
UINavigationController *theNavigationController = [[UINavigationController alloc] 
                                                           initWithRootViewController:chartViewController];
[chartViewController release];
[localViewControllersArray addObject:theNavigationController];
[theNavigationController release];

tabBarController.viewControllers = localViewControllersArray;

ChartThemeViewController是ThemeViewController的子类.ThemeViewController是UIViewController的子类.

The ChartThemeViewController is a subclass of ThemeViewController. ThemeViewController is a subclass of UIViewController.

如果我在ThemeViewController的子类中覆盖'shouldAutorotateToInterfaceOrientation',并且在所有子类中返回YES,在ChartThemeViewController中返回NO ...,那么所有ViewController都不会自动旋转.

If I override 'shouldAutorotateToInterfaceOrientation' in the subclasses of ThemeViewController, and return YES in all subclasses and return NO in ChartThemeViewController... it happens that all the ViewControllers don't autorotate.

mmmmhhh ...希望您能理解这一点...

mmmmhhh... hope you can understand this...

我该如何解决这个问题?

How can I solve this problem?

非常感谢詹斯

推荐答案

我相信在UITabBarController实例中进行选择性自动旋转的技巧如下:子类UITabBarController并重写方法-(BOOL)shouldAutorotateToInterfaceOrientation:,并具有以下内容:

I believe the trick to selective autorotation within an instance of UITabBarController is as follows: Subclass UITabBarController and override the method -(BOOL)shouldAutorotateToInterfaceOrientation: with the following:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return [[self selectedViewController] shouldAutorotateToInterfaceOrientation:interfaceOrientation];
};

现在,如果选择仅纵向"标签并旋转为横向,则应用程序将保持纵向状态.

Now, if you select your portrait-only tab and rotate to landscape, the application will stay in portrait.

这是一个新问题,如下所示:选择一个确实支持景观的标签;旋转到风景;选择一个仅支持纵向的标签.哦哦纵向显示的视图控制器以横向显示.不幸的是,无法将视图控制器强制设置为特定方向.

A new problem here, though, is as follows: select a tab that does support landscape; rotate to landscape; select a tab that only supports portrait. Uh oh. The portrait-only view controller is being displayed in landscape. Unfortunately, there is no way to force a view controller to a specific orientation.

您可以在此处查找问题的描述:

You can look here for a description of the problem: How to force a screen orientation in specific view controllers?

如您所见,没有真正的解决方案.

As you can see, there is no real solution.

我建议禁用所有不支持当前方向的标签.您可以使用以下方法覆盖UITabBarController子类中的-(void)didAutorotateToInterfaceOrientation:来实现此目的:

I would recommend disabling all tabs that do not support the current orientation. You can override -(void)didAutorotateToInterfaceOrientation: in your UITabBarController subclass with the following to achieve this:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    NSArray *items = [[self tabBar] items];
    for (NSUInteger i = 0; i < [items count]; i ++)
    {
        UITabBarItem *item = [items objectAtIndex:i];
        if (![[[self viewControllers] objectAtIndex:i] shouldAutorotateToInterfaceOrientation:toInterfaceOrientation])
        {
            [item setEnabled:NO];
        }
        else
        {
            [item setEnabled:YES];
        };
    };
};

如果您不想这样做,请考虑更改您的界面,使其能够在所有视图控制器中支持相同的方向.

If you do not wish to do this, then consider altering your interface to be able to support the same orientations in all of your view controllers.

希望这会有所帮助,

瑞安

这篇关于UIViewController旋转问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆