美文网首页
Swift中某个界面横屏设置。

Swift中某个界面横屏设置。

作者: Distance先生 | 来源:发表于2017-08-14 20:33 被阅读0次

    1.在Appdelegate中:

    // 是否横屏
    var isLandscape = false
    // MARK: 是否横屏
    extension AppDelegate {
        func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
            if isLandscape {
                return .all
            }
            else {
                return .portrait
            }
        }
    }
    

    2.在需要横屏的界面:

        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.isLandscape = true
            let value = UIInterfaceOrientation.landscapeRight.rawValue
            UIDevice.current.setValue(value, forKey: "orientation")
    
        }
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.isLandscape = false
    
             // MARK: - 这句很重要 
            let unknownValue = UIInterfaceOrientation.unknown.rawValue
            UIDevice.current.setValue(unknownValue, forKey: "orientation")
    
            // 将视图还原成竖屏
            let orientationTarget = UIInterfaceOrientation.portrait.rawValue
            UIDevice.current.setValue(orientationTarget, forKey: "orientation")
        }
    
        // 是否支持自转 看具体需求
        override var shouldAutorotate: Bool {
            return false
        }
        
        // 设置横屏方向
        override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            // 横屏 home键在右
            return .landscapeRight
        }
    
    

    相关文章

      网友评论

          本文标题:Swift中某个界面横屏设置。

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