一、如果要项支持屏幕横竖屏切换,首先要确保程序的总开关开启,勾选所要支持的Device Orientation。注意如果这里没有勾选,代码里面再怎么设置都是不会实现横竖屏转换的。
二、假设应用需要支持正竖屏幕/左横屏/右横屏,即对1、3、4项打勾选。这样之后如果程序中的ViewController使用的是系统默认的导航栏控制器,即UINavagationController,那么就能够实现屏幕切换了。
三、如果程序中使用的是第三方库提供的(或者是自己写的)导航栏控制器,这个导航栏控制器继承UINavagationController,如果不重写其横竖屏方面的代码,那么效果就和UINavagationController,同样是支持横竖屏切换的。
四、对于非系统导航栏控制器,要想对横竖屏切换进行控制,需要添加以下两个方法(这两个方法继承自UINavagationController):
//支持的屏幕方向
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
//是否允许改变横竖屏(开关)
- (BOOL)shouldAutorotate
{
return YES;
}
1、
- (NSUInteger)supportedInterfaceOrientations
该方法返回所要支持的屏幕方向
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),
};
横竖屏常量定义在该枚举中,需要注意的是ios6.0之后才适用。6.0之前有另外的枚举常量可支持
2、
- (BOOL)shouldAutorotate
该方法相当于开关,如想要支持,return YES,反之为NO。
可想而知,UINavagationController中返回的值为YES。