美文网首页
iOS强制横屏或者竖屏

iOS强制横屏或者竖屏

作者: OrekiSei | 来源:发表于2018-07-11 12:08 被阅读7次

    在AppDelegate中:

    1.添加属性:

    var isForcedLandscape = false  //标记是否横屏

    2.重写代理方法:

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

        if self.isForcedLandscape {

            return UIInterfaceOrientationMask.landscapeRight

        }

            return UIInterfaceOrientationMask.portrait

    }

    写两个全局方法:

    func setHorizontalScreen() {  //需要横屏时调用此方法

        isForcedLandscape = true

        let value = UIInterfaceOrientation.landscapeRight.rawValue

        UIDevice.current.setValue(value, forKey: "orientation")

    }

    func setVerticalScreen(){  //需要竖屏时调用此方法

        isForcedLandscape = false

        let value = UIInterfaceOrientation.portrait.rawValue

        UIDevice.current.setValue(value, forKey: "orientation")

    }

    相关文章

      网友评论

          本文标题:iOS强制横屏或者竖屏

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