IOS 屏幕旋转容易遇到的小问题

//Interface的方向是否会跟随设备方向自动旋转,如果返回NO,后两个方法不会再调用

- (BOOL)shouldAutorotate {returnYES;}

//返回直接支持的方向

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{returnUIInterfaceOrientationMaskPortrait;}

//返回最优先显示的屏幕方向

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {returnUIInterfaceOrientationPortrait;}


这三个方法是iOS6之后才有的,有个特点是只在两种情况下才生效

1.当前viewController是window的rootViewController。

2.当前viewController是modal模式的,即此viewController是调用presentModalViewController而显示出来的.

在以上两种情况中,supportedInterfaceOrientations的方法会作用于当前的viewController和所有的childViewController,如果不是以上的两种情况下的viewController,UIKit不会执行上述方法。

其它的情况可以采取这种类似方式:

在UINavigationController或UITabBarController里重写

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{ 

return self.selectedViewController.supportedInterfaceOrientations;

}

-(BOOL)shouldAutorotate{

return [self.selectedViewController shouldAutorotate];

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ 

return [self.selectedViewController preferredInterfaceOrientationForPresentation];

}

UINavigationController使用self.topViewController

UITabBarController使用self.selectedViewController

然后在viewController重写这三个方法,这样就巧妙的绕开了UIKit只调用rootViewController的方法的规则. 把决定权交给了当前正在显示的viewController.

参考文章:http://www.jianshu.com/p/e473749f1c30

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容