iOS强制横屏或者竖屏

在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")

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。