简体   繁体   中英

UIViewController rotating problem

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;

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

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... hope you can understand this...

How can I solve this problem?

many thanks jens

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.

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.

Hope this helps,

Ryan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM