TabBarController: Orienting views in different orientations

拟墨画扇 提交于 2019-12-12 21:03:33

问题


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.



来源:https://stackoverflow.com/questions/24479184/tabbarcontroller-orienting-views-in-different-orientations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!