参考:
http://www.superqq.com/blog/2015/12/07/ios9-interface-orientation/
http://www.jianshu.com/p/650ba0ff626b#
新建项目,默认支持横竖屏,如下:

Portrait 竖屏
Upside Down 上下翻转
Landscape Left 向左横屏(Home键在 )
Landscape Right 向右横屏(Home键在 )
在实际测试中,Landscape Left的效果是屏幕向右倾倒(Home键在左)即可实现横屏,Landscape Right的效果是屏幕向左倾倒(Home键在右)即可实现横屏。跟我想象的相反。
现在的项目一般都支持iOS7以上,所以现在的有关屏幕横竖屏的方法有如下三个(一般只需要用到前两个):
//是否允许自动旋转
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
//返回支持的旋转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
//返回优先方向
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
关于旋转方向的参数有如下,可根据具体情况具体设置:
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
} __TVOS_PROHIBITED;
下面根据具体情况,具体设置
情况一,项目每个页面都支持横竖屏
无需设置,默认即可。
情况二,项目某几个页面支持横竖屏,其他只支持竖屏。(每个页面均继承自自定义的UIViewController)
步骤:
1.在General -> Deployment info -> Device Orientation勾选Portrait、Landscape Left、Landscape Right以使得整个项目允许横屏显示。
2.在父类中:
-(BOOL)shouldAutorotate{
return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;//竖屏
}
3.在需要横屏的子类中:
-(BOOL)shouldAutorotate{
return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown;//横竖屏
}
情况三,UINavigationController作为项目的rootViewController,某几个页面支持横竖屏,其他只支持竖屏。(每个页面均继承自自定义的UIViewController)
除了情况二的代码,还需要自定义UINavigationController,加上如下代码:
-(BOOL)shouldAutorotate{
return [self.topViewController shouldAutorotate];
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return [self.topViewController supportedInterfaceOrientations];
}
网友评论