美文网首页
APP基础功能的配置管理之屏幕旋转控制

APP基础功能的配置管理之屏幕旋转控制

作者: 花果山松鼠 | 来源:发表于2018-04-19 16:06 被阅读65次

前言:
1.屏幕分为:横竖屏。
2.在苹果系统中,分为4个类型:Portrait(竖直正方向)、Upside Down(竖直倒转方向)、 LandscapeLeft(左)、LandscapeRight(右)
一般情况下,我们创建一个app,系统默认选择的是1、3、4方向,倒转方向用的很少,如果需要使用2方向,即竖直倒转方向,除了info.plist设置之外,还需在视图控制器里设置shouldAutorotate为NO;

// 是否默认旋转
- (BOOL)shouldAutorotate {
    return NO;
}

横屏用的地方也比较多,比如视频app从竖屏切到横屏全屏观看模式,再比如某些app的横屏页面板块。这就需要进行页面的横屏设置了。

这里还涉及到一个跳转问题,iOS页面跳转分为模态视图跳转Modal Presentation和通过导航栏的跳转

1.通过模态视图跳转,只需要在destination控制器里加上以下代码配置。
- (BOOL)shouldAutorotate {
    return NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return toInterfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeRight;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight;
}
2.通过导航栏的跳转push到下个控制器

在Appdelegate.h里添加外部属性

@property(nonatomic,assign)BOOL allowRotation; //是否允许转向

在Appdelegate.m里设定整个app的Orientation属性。

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    
    if (self.allowRotation == YES) {
        //横屏
        return UIInterfaceOrientationMaskLandscape;
    }else{
        //竖屏
        return UIInterfaceOrientationMaskPortrait;
    }
}

在目标控制器里,

AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//允许转成横屏
appDelegate.allowRotation = YES;
//调用横屏代码
[UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight]; //设定切换模式,如果在手机里取消了屏幕旋转锁定,那么横屏时就会根据手机的放置方向而自动旋转。

相关文章:
APP基础功能的配置管理之ATS
APP基础功能的配置管理之URL Scheme和白名单
APP基础功能的配置管理之系统权限控制

相关文章

网友评论

      本文标题:APP基础功能的配置管理之屏幕旋转控制

      本文链接:https://www.haomeiwen.com/subject/xhvwkftx.html