iOS设备旋转(更新)

1 配置

//appDelegate,默认是plist里面UIInterfaceOrientation决定(Build Settings->General设置)。
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
      //优先级高于info.plist
}

系统没有关闭自动旋转时

//UIViewController.m
#pragma mark - rotation
//定义是否支持自动旋转
-(BOOL)shouldAutorotate {
    return YES;
}
//定义支持旋转的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return isPad ? UIInterfaceOrientationMaskLandscape : UIInterfaceOrientationMaskPortrait;
}
//定义第一次进来的时候的方向
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return isPad ? UIInterfaceOrientationLandscapeLeft : UIInterfaceOrientationPortrait;
}

2 生命周期

//controller
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    if (size.width > size.height) {
        //横屏设置,为防止遮挡键盘,调整输入视图的高度
        self.textView_height.constant = 50;
    }else{
        //竖屏设置
        self.textView_height.constant = 200;
    }
}

//子视图
- (void)layoutSubviews {
    [super layoutSubviews];

    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationMaskPortrait) {
        //竖屏布局
    } else {
        //横屏布局
    }
}

3 设备方向&屏显方向

UIDeviceOrientation
机器硬件方向,只读,可以通过KVC设置
    [UIDevice currentDevice].orientation
UIInterfaceOrientation
程序界面方向,属性只读,有set方法可写。
    [UIApplication sharedApplication].statusBarOrientation

注意

  1. 注意弹框View与旋屏
  2. 横竖屏分辨率数
//iOS 8之后
//    竖屏:  
    UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)  
//    横屏:  
    UIScreen.mainScreen().bounds: (0.0,0.0,568.0,320.0) 

iOS横竖屏旋转及其基本适配方法

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • /* UIViewController is a generic controller base class th...
    DanDanC阅读 5,948评论 0 2
  • 你可以问什么问题,能让你和你的伴侣觉得自己是世界上最幸运的人? 很多人都有爱情保鲜的秘诀,比如我知道龚琳娜老师喜欢...
    然妈Miya阅读 2,701评论 0 4
  • 对句:天下见闻观则广【依岚听雪】 出句:世间学问悟为高【東方墨耕】 ———————————————---- 江山在...
    依嵐聽雪阅读 3,196评论 0 1
  • 三国中有一个人绝对是大名鼎鼎的,不过这所谓的名声不是什么好的名声,而是恶名,暴名!正是因为他的出现,汉朝延续了几百...
    夕阳褪去时阅读 3,693评论 0 1

友情链接更多精彩内容