当您将新视图推送到UINavigationViewController时,如果您希望强制横向方向,可以使用以下方法:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return .landscape
}
这将确保您的应用程序始终使用横向方向。
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .landscape
}
这将确保您的视图控制器始终使用横向方向。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
这将确保当视图控制器出现时,它将使用横向方向,当视图控制器消失时,它将使用纵向方向。
请注意,这些方法可能会被视为私有API,因此在将其用于生产应用程序时要小心。
领取专属 10元无门槛券
手把手带您无忧上云