系统支持横屏顺序
默认读取plist里面设置的方向(优先级最高)等同于Xcode Geneal设置里面勾选
application window设置的级别次之
然后是UINavigationcontroller/UITabbarController
级别最低的是viewcontroller
(注意Xcode Geneal设置里面没有勾选的方向viewcontroller强制旋转到该方向会crash)
旋转屏幕相关的几个重要方法:
//是否能旋转屏幕
- (BOOL)shouldAutorotate {
return YES;
}
//支持旋转的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
//强制旋转屏幕,私有方法 direction:UIInterfaceOrientation
NSNumber * value = [NSNumber numberWithInteger:direction];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
几个旋转屏幕的小技巧:
1.项目中大多数页面都不支持旋转在rootViewController中shouldAutorotate方法中返回NO即可,对于支持旋转的我们使用presentViewController方法推出页面。
2.从支持旋转的页面A,push到不支持旋转的页面B 这里实现一个UINavigationController类重写一下方法:
//是否能旋转屏幕
- (BOOL)shouldAutorotate {
return self.topViewController.shouldAutorotate;
}
//支持旋转的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
在B页面shouldAutorotate方法中返回NO,推出B页面之前强制旋转为竖屏即可。
3.在设备已经旋转为横屏的状态下,从竖屏A,present到支持横屏的页面B,发现推出的直接是横屏,为了交互的一致性,希望推出的还是竖屏,可以在viewDidLoad,和viewDidAppear方法中先后设置self.needAutorotate为NO,和YES
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if (self.needAutorotate == YES) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
return UIInterfaceOrientationMaskPortrait;
}
4.设备强制旋转后键盘弹出的方向不对,一定是supportedInterfaceOrientations返回的参数不对,调试下就ok了5.对于一会可以自由旋转一会不能旋转的需求也是对supportedInterfaceOrientations返回参数做文章。