美文网首页
iOS项目中,只有个别控制器允许横屏

iOS项目中,只有个别控制器允许横屏

作者: oneYing | 来源:发表于2018-08-06 14:29 被阅读7次

    首先要允许项目支持横竖屏

    一般的项目都会用到navigationController与tabBarController,需要在这二者中先做一些设置

    
    1.navigationController中重写下面有一个方法
    
    
    
    -(BOOL)shouldAutorotate
    
    {
    
    return YES;
    
    }
    
    -(UIInterfaceOrientationMask)supportedInterfaceOrientations
    
    {
    
    return [self.viewControllers.lastObject supportedInterfaceOrientations];
    
    }
    
    -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    
    {
    
    return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
    
    }
    
    

    2.tabBarController中也重写上述三个方法,得实现时有所不同

    
    - (BOOL)shouldAutorotate
    
    {
    
    return [self.selectedViewController shouldAutorotate];
    
    }
    
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations
    
    {
    
    return [self.selectedViewController supportedInterfaceOrientations];
    
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    
    {
    
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
    
    }
    
    

    3.在只支持竖屏的controller中也要重写上述方法,支持横屏的controller中不做操作

    
    -(UIInterfaceOrientationMask)supportedInterfaceOrientations
    
    {
    
    return UIInterfaceOrientationMaskPortrait ;
    
    }
    
    - (BOOL)shouldAutorotate
    
    {
    
    return NO;
    
    }
    
    -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    
    {
    
    return UIInterfaceOrientationPortrait;
    
    }
    
    

    如果设置了还不成功。那就有可能是 statusbar style 没有设置,要把Requre full screen 勾选上
    demo地址 https://github.com/handnaying/-.git

    相关文章

      网友评论

          本文标题:iOS项目中,只有个别控制器允许横屏

          本文链接:https://www.haomeiwen.com/subject/mfxgvftx.html