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