Iphone和iPad适配, 横竖屏
竖屏情况下: [UIScreen mainScreen].bounds.size.width = 320
[UIScreen mainScreen].bounds.size.width = 568
横屏情况下:
[UIScreen mainScreen].bounds.size.width = 568
[UIScreen mainScreen].bounds.size.height = 320
UIViewController 和 UIWindow同理
1. storyboard iphone和 iPad版
即为iphone和ipad分别创建xib/storyboard文件
Main_iPhone.storyboard
Main_iPad.storyboard
横竖屏
苹果官方文档:
When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. If the view controller supports the new orientation, the window and view controller are rotated to the new orientation. This method is only called if the view controller's shouldAutorotate method returns YES.
- (BOOL)shouldAutorotate {
return YES; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAll; }
注意要写在最顶层视图控制器, (self.window.rootViewController) , 否则不会进入以上方法
而且
supportedInterfaceOrientations的返回值必须是
UIInterfaceOrientationMaskLandscapeLeft 或者 UIInterfaceOrientationMaskLandscapeRight 或者...
总之必须加Mask
通知是当前线程异步
tabbarcontroller 为 根控制器的情况
- (BOOL)shouldAutorotate { return [self.selectedViewController shouldAutorotate]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [self.selectedViewController preferredInterfaceOrientationForPresentation]; } - (NSUInteger)supportedInterfaceOrientations { return [self.selectedViewController supportedInterfaceOrientations]; }
navigationcontroller为根控制器的情况:
- (BOOL)shouldAutorotate { return [self.viewControllers.lastObject shouldAutorotate]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation]; } - (NSUInteger)supportedInterfaceOrientations { return [self.viewControllers.lastObject supportedInterfaceOrientations]; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· 盘点!HelloGitHub 年度热门开源项目
· 某Websocket反爬逆向分析+请求加解密+还原html
· DeepSeek V3 两周使用总结
· 02现代计算机视觉入门之:什么是视频
· 回顾我的软件开发经历:我与代码生成器的涅槃之路