iOS部分页面横屏显示
在iOS系统支持横屏顺序
默认读取plist里面设置的方向(优先级最高)等同于Xcode Geneal设置里面勾选
application window设置的级别次之
然后是UINavigationcontroller/UITabbarController
级别最低的是viewcontroller
(注意Xcode Geneal设置里面没有勾选的方向viewcontroller强制旋转到该方向会crash)
通常设置部分页面横屏有两种方法,其一是在viewcontroller设置如下方法
-(BOOL)shouldAutorotate{ //屏幕是否可以旋转 return YES; } -(UIInterfaceOrientationMask)supportedInterfaceOrientations{ //支持的旋转方向 return UIInterfaceOrientationMaskLandscape; } -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ //初始显示的方向 return UIInterfaceOrientationLandscapeRight; }
如果你的应用页面都是present的这些就够了,然而并不会,所以重点在下面
上面已经说过了横屏顺序,所以还要支持UINavigationcontroller,UITabbarController
在UINavigationcontroller中
- (BOOL)shouldAutorotate { return [self.topViewController shouldAutorotate]; } - (NSUInteger)supportedInterfaceOrientations { return [self.topViewController supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [self.topViewController preferredInterfaceOrientationForPresentation]; }
在UITabbarController中
- (BOOL)shouldAutorotate { return [self.selectedViewController shouldAutorotate]; } - (NSUInteger)supportedInterfaceOrientations { return [self.selectedViewController supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [self.selectedViewController preferredInterfaceOrientationForPresentation]; }
这就完了,并没有,你会发现我都设置了,为什么present的页面都可以横屏,为啥push的不行了,呵呵
所以你要在横屏的页面中再加上一些东西
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //强制横屏 NSNumber *value = [NSNumber numberWithInt:UIDeviceOrientationLandscapeLeft]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; [UIViewController attemptRotationToDeviceOrientation]; }
在当前页面完全显示出来由某些方法触发的情况下来强制横屏或竖屏,上面的代码并不起作用
还有一种强制横屏的方法如下
-(void)resetDeviceOrientation{ dispatch_async(dispatch_get_main_queue(), ^{ if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { SEL selector = NSSelectorFromString(@"setOrientation:"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:[UIDevice currentDevice]]; int val = UIInterfaceOrientationPortrait; [invocation setArgument:&val atIndex:2]; [invocation invoke]; } }); }
到这里就基本完成了
不知道你发现没有在push情况下preferredInterfaceOrientationForPresentation方法并不起作用所以这个可以不写
其二在APPDelegate中设置,具体可以参照
https://blog.csdn.net/SandyLoo/article/details/52044190