在iOS6之后,若是APP须要支持横屏,须要在xcode设置中General里面进行勾选配置:xcode
配置完成以后,咱们能够看一下Info.plist里面的Supported interface orientations选项也相应的改变了。以下图:app
固然,咱们也能够直接在Info.plist进行配置。ide
在iOS6以前咱们能够直接用这个方法进行配置:测试
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0) __TVOS_PROHIBITED;
在iOS6以后,这个方法被NS_DEPRECATED_IOS,也就是废弃掉了。废弃了这个方法,苹果相应的也给出了新的方法来代替:spa
// New Autorotation support. - (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED; - (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
咱们能够看到iOS6以前是一个方法,在iOS6以后变成两个方法了,一个是是否旋转的方法,一个是支持的方向的方法。code
假设:咱们ViewController是直接加载window的self.window.rootViewController上面的。代码以下:it
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; ViewController *vc = [[ViewController alloc] init]; self.window.rootViewController = vc; [self.window makeKeyAndVisible]; return YES; }
若是咱们要是想支持上面General里勾选的方向(竖屏、横屏向左已经横屏向右)该如何实现呢?首先,咱们应该设置让他支持旋转,而后在设置支持的方向。代码以下:io
//支持旋转 -(BOOL)shouldAutorotate{ return YES; } //支持的方向 - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAllButUpsideDown; }
其中UIInterfaceOrientationMask是一个枚举:import
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;
能够根据本身的需求来选择。上面咱们说了假设这个条件,若是rootViewController上导航,咱们直接在ViewController里面设置,这个方法就不灵了。(你们能够本身测试一下)配置
为何是导航上面的方法就不灵了呢?缘由很简单,咱们没有设置导航支持的方向。别忘了UINavigationController也是UIViewController的子类。须要受到一样的待遇的。
如何设置呢?咱们能够建立一个UINavigationController的子类,假设叫GGPublicNavigationViewController。而后,咱们在GGPublicNavigationViewController.m文件里面也实现着两个方法:
//支持旋转 -(BOOL)shouldAutorotate{ return YES; } //支持的方向 - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAllButUpsideDown; }
这样设置以后,即便咱们push进去的UIViewController没有实现上面的连个方法,也是能够支持横屏的。也就是说,咱们push的全部都支持横屏。这个作法是否是很暴力!
有些童鞋会问了,如何控制每一个界面支持的方向呢?这也是能够办到的,在GGPublicNavigationViewController不能写死支持哪一个。咱们能够这么写:
-(BOOL)shouldAutorotate{ return [self.topViewController shouldAutorotate]; } //支持的方向 - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return [self.topViewController supportedInterfaceOrientations];; }
self.topViewController是当前导航显示的UIViewController,这样就能够控制每一个UIViewController所支持的方向啦!
好啦,关于iOS9中横竖屏的处理就说这么多吧!(其实iOS七、iOS8也是这么设置的)若是你以为文章还不错,分享一下吧!