tabBarController and navigationControllers in landscape mode, episode II

前端 未结 7 2010
轻奢々
轻奢々 2020-12-08 09:11

I have a UITabBarController, and each tab handles a different UIViewController that pushes on the stack new controllers as needed. In two of these tabs I need, when a specif

7条回答
  •  庸人自扰
    2020-12-08 09:23

    I ran into the same issues as you did when working with the UITabBarController. I needed to control which UIViewControllers were allowed to rotate and which were not. My main problem was with the MORE tab. I did not want any of the UIViewControllers included in the MORE tab to rotate.

    My solution was to create my own UITabBarController which I called MyTabBarController:

    @interface MyTabBarController : UITabBarController  {
    
    }
    

    Then I implemented the shouldAutorotateToInterfaceOrientation method:

    @implementation MyTabBarController
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     UIViewController *controller = [self selectedViewController];
    
     if ((controller == [self moreNavigationController]) || ([self selectedIndex] == 4))
     {
      return interfaceOrientation == UIInterfaceOrientationPortrait;
     }
    
     return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }
    
    @end
    

    I needed to discover if the MORE tab was selected. This is a two step process; when the MORE tab is selected initially the API returns a selectedIndex higher than 4 so I needed to compare the selected controller with the moreNavigationController.

    If an UIViewController is selected from within the MORE tab then the selectedIndex is finally 4 but the selectedController is not the moreNavigationController anymore but the UIViewController selected.

    The if ((controller == [self moreNavigationController]) || ([self selectedIndex] == 4)) takes care of this issue.

    Now, when I run my application my UIViewControllers in the MORE tab are not rotated. I hope this will help other developers who are running into the same issues as I did.

    Emilio

提交回复
热议问题