美文网首页iOS-Developer-SwiftiOS学习Swift
Swift 4.2 禁止旋转屏幕-强制屏幕旋转

Swift 4.2 禁止旋转屏幕-强制屏幕旋转

作者: JasonFive | 来源:发表于2018-11-22 11:38 被阅读912次

禁止旋转屏幕,有两种方法

1、直接Xcode写死,简单方便 (不推荐)
1542854748635.jpg

但是这样有个问题,当需要旋转屏幕或者,在倒入某些第三方的时候,如果不支持屏幕旋转,在使用第三方的时候应用会蹦。所以,这样写-不推荐


2、用代码控制屏幕旋转与否
override var shouldAutorotate: Bool
override var supportedInterfaceOrientations: UIInterfaceOrientationMask

这两个方法就可以控制屏幕的旋转和不旋转了

a、在Appdelegate中设置三个属性
var isForceLandscape:Bool = false
var isForcePortrait:Bool = false
var isForceAllDerictions:Bool = false //支持所有方向

然后重写 func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?)

/// 设置屏幕支持的方向
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if isForceAllDerictions == true {
            return .all
        } else if isForceLandscape == true {
            return .landscape
        } else if isForcePortrait == true {
            return .portrait
        }
        return .portrait
    }

到这儿,就已经可以禁止屏幕旋转了

b、强制旋转横屏

在需要强制旋转屏幕的VC中添加以下代码就可以实现屏幕强制横屏

    /// 屏幕旋转
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //self.setNavigationBarHidden(hidden: true)
        forceOrientationLandscape()
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        //self.setNavigationBarHidden(hidden: false)
        forceOrientationPortrait()
    }
    override var shouldAutorotate: Bool {
        get {
            return true
        }
    }
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        get {
            return .landscapeRight
        }
    }
    // 强制旋转横屏
    func forceOrientationLandscape() {
        let appdelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
        appdelegate.isForceLandscape = true
        appdelegate.isForcePortrait = false
        _ = appdelegate.application(UIApplication.shared, supportedInterfaceOrientationsFor: view.window)
        let oriention = UIInterfaceOrientation.landscapeRight // 设置屏幕为横屏
        UIDevice.current.setValue(oriention.rawValue, forKey: "orientation")
        UIViewController.attemptRotationToDeviceOrientation()
    }
    // 强制旋转竖屏
    func forceOrientationPortrait() {
        let appdelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
        appdelegate.isForceLandscape = false
        appdelegate.isForcePortrait = true
        _ = appdelegate.application(UIApplication.shared, supportedInterfaceOrientationsFor: view.window)
        let oriention = UIInterfaceOrientation.portrait // 设置屏幕为竖屏
        UIDevice.current.setValue(oriention.rawValue, forKey: "orientation")
        UIViewController.attemptRotationToDeviceOrientation()
    }

这样当进入当前VC的时候就可以实现横屏,退出当前VC为竖屏
由于时间关系,目前就先写这么多,后续再更新

相关文章

  • Swift 4.2 禁止旋转屏幕-强制屏幕旋转

    禁止旋转屏幕,有两种方法 1、直接Xcode写死,简单方便 (不推荐) 但是这样有个问题,当需要旋转屏幕或者,在倒...

  • Swift 禁止屏幕旋转

    禁止屏幕旋转 在主控制器文件中添加如下代码: 敲重点禁止屏幕旋转的原理是覆写了控制器类(如果是新建的项目为View...

  • Swift小技巧(持续更新)

    1.Swift强制旋转屏幕设置 关键代码: 未完待续~~~

  • iOS 部分界面强制横屏与强制竖屏

    最新屏幕强制旋转详见 强制横屏(此方法为旋转视图) 恢复竖屏

  • 强转屏幕方向

    //强制旋转屏幕 (void)orientationToPortrait:(UIInterfaceOrientat...

  • swift 屏幕旋转

    竖直项目中遇到在某个页面支持横屏第一步:AppDelegate中设置一个变量标记是否要旋转var allowRot...

  • 强制屏幕旋转方向

    原文地址:https://stackoverflow.com/questions/40413567/overrid...

  • iOS 强制旋转屏幕

    项目需求: 我只是以第一个需求为例,第二种情况,道理相同。 无需强行选中 Device Orientation 中...

  • 如何强制旋转屏幕

    转:http://adad184.com/2014/09/19/how-to-force-rotate-scree...

  • iOS 强制旋转屏幕

    前言 在开发中有时会碰到旋转屏幕的需求,例如直播时横竖屏推流,这里我使用的一种方法时用纯代码强制翻转,其他晚上方法...

网友评论

    本文标题:Swift 4.2 禁止旋转屏幕-强制屏幕旋转

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