美文网首页
强制屏幕旋转方向

强制屏幕旋转方向

作者: 白色天空729 | 来源:发表于2018-07-17 16:29 被阅读15次

    原文地址:https://stackoverflow.com/questions/40413567/overriding-shouldautorotate-not-working-in-swift-3

    var orientationLock = UIInterfaceOrientationMask.all
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return self.orientationLock
    }
    
    struct AppUtility {
        static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
            if let delegate = UIApplication.shared.delegate as? AppDelegate {
                delegate.orientationLock = orientation
            }
        }
    
        static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
            self.lockOrientation(orientation)
            UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
        }
    }
    

    使用:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //let value = UIInterfaceOrientation.landscapeLeft.rawValue
        //UIDevice.current.setValue(value, forKey: "orientation")
        AppDelegate.AppUtility.lockOrientation(.landscapeLeft)
    
    }
    

    相关文章

      网友评论

          本文标题:强制屏幕旋转方向

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