在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:]; [invocation invoke];
}
}); }

到这里就基本完成了

不知道你发现没有在push情况下preferredInterfaceOrientationForPresentation方法并不起作用所以这个可以不写

其二在APPDelegate中设置,具体可以参照

https://blog.csdn.net/SandyLoo/article/details/52044190

最新文章

  1. linux架构图
  2. 李洪强iOS开发之静态库
  3. 如何对SQL Server 2005进行设置以允许远程连接(转载)
  4. json串的使用
  5. SQL Server 已提交读快照 测试
  6. java中memcached
  7. c语言贪吃蛇详解3.让蛇动起来
  8. Java深入了解TreeSet
  9. Python:当你遇到了the package “public”?
  10. cocos2dx lua invalid 'cobj' in function 'lua_cocos2dx'
  11. Elasticsearch-6.7.0系列(一)9200端口 .tar.gz版本centos7环境--下载安装运行
  12. Docker实现运行tomcat并部署项目war包,并实现挂载目录
  13. css居中小结
  14. SSAS知识回放之订单数据分析
  15. iOS开发transform的使用
  16. Angular CLI 安装和使用以及安装失败的解决方法
  17. UBuntu14.04下安装和卸载Qt5.3.1
  18. PyQt4消息窗口
  19. 002-and design-基于dva的基本项目搭建
  20. 【redis】redis五大类 用法 【转载:https://www.cnblogs.com/yanan7890/p/6617305.html】

热门文章

  1. qq群的表设计探究
  2. RPC_E_SERVERFAULT excel com操作错误
  3. ASP.NETCore -----导出Excel文件并下载
  4. ffmpeg 知识点
  5. angularJS 服务
  6. node - 路由的使用
  7. 读取docx表格中的信息
  8. ActorFramework教程对比及规划
  9. 201771010142-张燕 实验一 软件工程准备—<软件工程的初步了解和学习目标>
  10. 吴裕雄--天生自然C++语言学习笔记:C++ 命名空间