TabBarController: Orienting views in different ori

2019-07-20 15:35发布

问题:

I am having trouble preserving my current views orientation. In the below setup, I have been able to lock the first view controller to portrait and the second viewcontroller to landscape or portrait. However, when I add a second navigation controller/rootviewcontroller to the tab controller, all of the views throughout the project will go to both landscape and portrait. This happens regardless of if I implement the same code in the first navigation controller to the second nav controller or not

I would like to be able to preserve my current view controllers orientations while adding an additional navcontroller>viewcontroller

I have the following setup in storyboard:

This is what I am trying to achieve:

Where the tabbarcontroller should support all orientations, the nav controllers support all orientations, the first view controller and table view controller supports only portrait, and the second view controller supports landscape and portrait.

And here are the methods for each of the current view controllers

TabViewController.m

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

NavController.m

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

First View Controller.m

-(BOOL)shouldAutorotate

{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{

    return UIInterfaceOrientationPortrait;
}

Second View Controller.m

-(BOOL)shouldAutorotate

{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{

    return UIInterfaceOrientationLandscapeLeft;
}

I would appreciate any assistance in resolving this. It has become the bane of my existence

回答1:

It seems that you've subclassed both navigation an tabbar controller. Instead of doing that you can control at runtime the orientations just using their delegation protocol.

– tabBarControllerSupportedInterfaceOrientations:
– tabBarControllerPreferredInterfaceOrientationForPresentation:

and

– navigationControllerPreferredInterfaceOrientationForPresentation:
– navigationControllerSupportedInterfaceOrientations:

You can put your logic in those method I think that would be better for having a more clear implementation, instead of subclassing. Container view controllers usually take over about single vc rotations implementations.