开发者社区> 问答> 正文

请问IOS中整个工程是设置了支持屏幕旋转,那么怎么禁止其中某个视图旋转呢?

如题,整个项目中其他视图都是可以旋转的,但是,现在希望其中一个视图只能保持纵向,不允许旋转。

用什么代码有效?

在要禁止的视图中,加入了下面的代码,无效。

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);


}
(BOOL)shouldAutorotate

{
return NO;


}
(NSUInteger)supportedInterfaceOrientations

{
return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向)


} 

展开
收起
爵霸 2016-03-11 08:06:41 2682 0
2 条回答
写回答
取消 提交回答
  • 不可以,但是通过通过加约束实现
    2019-07-17 18:58:15
    赞同 展开评论 打赏
  • UINavigationController添加一个分类,然后重新构造函数

     #import "UINavigationController+Category.h"
    
    @implementation UINavigationController (Category)
    
    (BOOL)shouldAutorotate {
     return [self.viewControllers.lastObject shouldAutorotate];
     }
    
    
    (NSUInteger)supportedInterfaceOrientations {
     return [self.viewControllers.lastObject supportedInterfaceOrientations];
     }
    
    
    (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
     return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
     }
    
    
    @end

    在需要不旋转的界面中加上,你上述的代码

    2019-07-17 18:58:15
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载