iOS 6 UITabBarController supported orientation with current UINavigation controller

后端 未结 4 1264
长情又很酷
长情又很酷 2020-11-30 21:35

I have an iPhone app I am updating to iOS 6 that is having rotation issues. I have a UITabBarController with 16 UINavigationCotrollers. Most of the

相关标签:
4条回答
  • 2020-11-30 22:10

    I think is better something like that (as a category method)

    -(NSUInteger) supportedInterfaceOrientations {
        if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
        {
            return [self.topViewController supportedInterfaceOrientations];
        }
        return UIInterfaceOrientationMaskPortrait;
    }
    

    this ensures that the method is implemented. If you aren't doing this check and the method is not implemented (like in iOS5 env) the app should crash!

    0 讨论(0)
  • 2020-11-30 22:12

    If you plan to enable or disable rotation for all view controllers you don't need to subclass UINavigationController. Instead use:

       -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
    

    in your AppDelegate.

    If you plan to support all orientations in your app but different orientations on PARENT View Controllers (UINavigationController stack for example) you should use

       -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
    

    from AppDelegate in combination with the following methods in your PARENT View Controller.

        - (BOOL)shouldAutorotate
    

    and

    - (NSUInteger)supportedInterfaceOrientations
    

    But if you plan to have different orientation settings in different CHILDREN ViewControllers in the same navigation stack (like me) you need to check the current ViewController in the navigation stack.

    I've created the following in my UINavigationController subclass:

        - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        int interfaceOrientation = 0;
    
        if (self.viewControllers.count > 0)
        {
            DLog(@"%@", self.viewControllers);
            for (id viewController in self.viewControllers)
            {
                if ([viewController isKindOfClass:([InitialUseViewController class])])
                {
                     interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
                }
                else if ([viewController isKindOfClass:([MainViewController class])])
                {
                     interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
                }
                else
                {
                     interfaceOrientation = UIInterfaceOrientationMaskAllButUpsideDown;
                }
            }
        }
        return interfaceOrientation;
    }
    

    Since you cannot control anymore from children ViewControllers the rotation settings of presented view controller you must somehow intercept what view controller is currently in the navigation stack. So that's what I did :). Hope that helps !

    0 讨论(0)
  • 2020-11-30 22:13

    Subclass your UITabBarController overriding these methods:

    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // You do not need this method if you are not supporting earlier iOS Versions
        return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        return [self.selectedViewController supportedInterfaceOrientations];
    }
    
    -(BOOL)shouldAutorotate
    {
        return YES;
    }
    

    Subclass your UINavigationController overriding these methods:

    -(NSUInteger)supportedInterfaceOrientations
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    
    -(BOOL)shouldAutorotate
    {
        return YES;
    }
    

    Then implement these methods in your viewControllers that you do not want to rotate:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    -(BOOL)shouldAutorotate
    {
        return NO;
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    

    And for viewControllers that you do want to rotate:

        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
        {
            return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
        }
    
        -(NSUInteger)supportedInterfaceOrientations
        {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    
        -(BOOL)shouldAutorotate
        {
            return YES;
        }
    

    Your tabbarController should be added as the RootviewController of the app window. If you plan to support the default orientations, all but upsidedown is default for iPhone, then you do not need to do anything else. If you want to support upside-down or if you do not want to support another of the orientations, then you need to set the appropriate values in app delegate and/or info.plist.

    0 讨论(0)
  • 2020-11-30 22:23

    I had issue that some View controllers in the navigation stack support all the orientations, some only portrait, but UINavigationController was returning all app supported orientations, this little hack helped me. I'm not sure if this is intended behavior or what

    @implementation UINavigationController (iOS6OrientationFix)
    
    -(NSUInteger) supportedInterfaceOrientations {
        return [self.topViewController supportedInterfaceOrientations];
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题