美文网首页
横屏处理OC&Swift4.1

横屏处理OC&Swift4.1

作者: 527267线的iOS工程师 | 来源:发表于2018-03-05 13:22 被阅读0次

首先我们要先设置app允许的窗口朝向

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window;

如果你没有写这个方法,那会从plist里获取默认值。
该方法来自UIApplicationDelegate,所以要写在AppDelegate里。
所使用的枚举
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait,//home键朝下
UIInterfaceOrientationMaskLandscapeLeft,//home键朝左
UIInterfaceOrientationMaskLandscapeRight ,//home键朝右
UIInterfaceOrientationMaskPortraitUpsideDown,//home键朝上
UIInterfaceOrientationMaskLandscape,//home键朝左或者朝右
UIInterfaceOrientationMaskAll,//上下左右都可以
UIInterfaceOrientationMaskAllButUpsideDown,//home键朝左朝右朝下
}

swift写法

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait.union(UIInterfaceOrientationMask.landscapeLeft);
//可以竖屏和home键在左边横屏
 }

OC写法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

然后设置页面是否允许翻转和页面允许的方向

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
//默认值iPad UIInterfaceOrientationMaskAll iPhone  UIInterfaceOrientationMaskAllButUpsideDown
- (BOOL)shouldAutorotate //默认值为YES
- (BOOL)prefersStatusBarHidden //默认值为YES

supportedInterfaceOrientations这个方法表示该view controller允许的朝向,但是这个方向必须是application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window中或者plist中允许的方向。
横屏的时候状态栏会默认隐藏。所以我们需要设置prefersStatusBarHidden
OC

- (BOOL)shouldAutorotate {
    return YES;
}

- (BOOL)prefersStatusBarHidden {
    return NO;
}  

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

Swift

    override var shouldAutorotate: Bool {
        return false
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait.union(UIInterfaceOrientationMask.landscapeRight.union(UIInterfaceOrientationMask.portraitUpsideDown))
    }
    
    override var prefersStatusBarHidden: Bool {
        return false
    }

这三个方法都是UIViewController下。也就是说UIViewcontroller及他的子类都可以用这三个方法(UITabBarController, UINavigationController也是UIViewcontroller的子类)。
如果rootViewController为UITabBarController实例的时候。
只有UITabBarController的shouldAutorotate起作用。UITabBarController下的UINavigationController和UIViewcontroller的shouldAutorotate都有不能控制窗口翻转,但是UIViewcontroller的shouldAutorotate为YES时可以造成状态栏的翻转,状态栏的翻转方向受UIViewcontroller的supportedInterfaceOrientations影响。
效果如下:


屏幕快照 2018-03-05 上午11.22.09.png

如果rootViewController为UINavigationController实例的时候。UIViewcontroller的shouldAutorotate为YES时可以造成状态栏的翻转但是不会对窗口的翻转有影响。状态栏的翻转方向受UIViewcontroller的supportedInterfaceOrientations影响。
由UIViewcontroller present出来的页面不受rootViewController的影响。翻转情况由他自己的shouldAutorotate和supportedInterfaceOrientations控制。

强制翻转:
当退出可翻转页面的时候我们往往会用到强制翻转。原理就是一个KVC。
OC

//先判断Orientation状态
if ((long)self.preferredInterfaceOrientationForPresentation != 1) {
        NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
        [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
        
        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
    }

Swift

//注意在swift中系统不能将enum直接转成int型,所以需要手动get rawValue
if self.preferredInterfaceOrientationForPresentation != .portrait {
        
         UIDevice.current.setValue(NSNumber.init(value: UIDeviceOrientation.unknown.rawValue), forKey: "orientation")
                    UIDevice.current.setValue(NSNumber.init(value: UIDeviceOrientation.portrait.rawValue), forKey: "orientation")
}

相关文章

  • 横屏处理OC&Swift4.1

    首先我们要先设置app允许的窗口朝向 如果你没有写这个方法,那会从plist里获取默认值。该方法来自UIAppli...

  • iOS 横竖屏处理

    开发中有竖屏和横屏的界面时,我们需要监听屏幕旋转,强制横屏,锁定方向后的屏幕强制旋转等处理.以下做个总结: 一.横...

  • 强制横屏问题处理

    最近需求是开发一款智能游戏控制器GameVController 放入app内,要求必须横屏显示:环境:1、TARG...

  • iOS开发单个页面横屏处理(swift版)

    问题 iOS开发中大部分情况都是只允许竖屏的,但是偶尔几个页面需要做横屏处理(或者只能横屏) 解决方案 Apple...

  • quick-cocos2d-x 多分辨率适配详解

    多种分辨率适配的原理 因为横屏和竖屏的原理完全相同,所以本文先以竖屏为例,后文再说明横屏的处理。 制作一张 640...

  • 关于 fragmentation 如何只让某个Fragment支

    众所周知,Android 如果用Activity做横屏处理是相当简单的,就一个配置解决,剩下的就是修改横屏相应的布...

  • 屏幕横竖屏处理

    在APP开发中,我们经常遇到某些界面需要横屏处理,一般情况是APP整体竖屏展示,只有极个别界面横屏界面。最近在做视...

  • 竖屏应用里面添加横屏页面

    需求:在某个竖屏应用里面有若干横屏。 处理方法1 UINavagationController由于当前的屏幕状态是...

  • iOS 屏幕旋转shouldAutorotate

    最近项目中有个分时图的显示需要进行横屏处理。因为整个项目里面大部分页面都是需要竖屏显示的。只有几个页面是横屏显示。...

  • iPhone屏幕尺寸、statusBar、navigationB

    iPhone型号状态栏导航栏tabBariPhone型号竖屏横屏竖屏横屏竖屏横屏iPhone 14 Pro Max...

网友评论

      本文标题:横屏处理OC&Swift4.1

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