swift 3 强制横屏

在要横屏的控制器下面设置:

override func viewWillAppear(_ animated: Bool) {

super.viewWillAppear(animated)

AppDelegate.isLandscape = true

let value = UIInterfaceOrientation.landscapeLeft.rawValue

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

}

override func viewWillDisappear(_ animated: Bool) {

super.viewWillDisappear(animated)

//将试图还原为竖屏

AppDelegate.isLandscape = false

let value = UIInterfaceOrientation.portrait.rawValue

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

}

在AppDelegate下面声明一个静态变量,添加代理UIApplicationDelegate的方法

class AppDelegate: UIResponder {

static var isLandscape:Bool = false

}


extension AppDelegate:UIApplicationDelegate {

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

if AppDelegate.isLandscape {

return UIInterfaceOrientationMask.landscapeLeft

}else{

return UIInterfaceOrientationMask.portrait

}

}

}

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

推荐阅读更多精彩内容