通用设备开发-多设备旋转适配

根据不同设备,设置应用支持不同的朝向:

iPhone应用除了视频类和游戏类,一般只需要支持一个竖屏模式就可以了,而iPad往往会全部进行支持,接下来就简单演示iPhone与iPad不同设备时的应用可支持朝向处理

1.先将工程的支持朝向全部勾选:

deployment info.png

2.在supportedInterfaceOrientationsForWindow中设置可支持朝向

// 当设置应用的可支持方向时调用
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    
    // 根据不同设备设置支持方向
    if (isiPhone) {
        
        // iPhone
        return UIInterfaceOrientationMaskPortrait;
        
    }else {
        
        // iPad
        return UIInterfaceOrientationMaskAll;
    }
    
}

为了提高代码可读性,这里将判断是否为iPhone设备抽取了一个宏:

#define isiPhone ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) 

这样当使用iPhone设备时,只支持竖屏显示,当使用iPad设备时,支持全部朝向

注意点:我们这是的是应用的可支持朝向,设备的摆放方向是由用户决定的

UIInterfaceOrientationMask枚举值:

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

相关阅读更多精彩内容

友情链接更多精彩内容