如何始终纵向加载应用程序?
How to load App in portrait all time?
应用程序在启动时必须是纵向的,但之后我可以旋转它。
如何做到这一点,也许以编程方式?
您可以将此添加到您希望在纵向模式下拥有的每个控制器:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait //return the value as per the required orientation
}
或者您可以在设置中进行设置(正如@Sid Mhatre 已经提到的):
您可以像这样在 appDelegate 中为方向创建一个扩展:
protocol MyAppDelegate: class {
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
}
extension AppDelegate: MyAppDelegate {
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
}
在您想要允许旋转的 viewController 中,您应该在 viewDidLoad 方法中实现它:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true
并在 viewWillDisappear 上再次删除它:
override func viewWillDisappear(_ animated: Bool) {
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = false
}
应用程序在启动时必须是纵向的,但之后我可以旋转它。
如何做到这一点,也许以编程方式?
您可以将此添加到您希望在纵向模式下拥有的每个控制器:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait //return the value as per the required orientation
}
或者您可以在设置中进行设置(正如@Sid Mhatre 已经提到的):
您可以像这样在 appDelegate 中为方向创建一个扩展:
protocol MyAppDelegate: class {
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
}
extension AppDelegate: MyAppDelegate {
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
}
在您想要允许旋转的 viewController 中,您应该在 viewDidLoad 方法中实现它:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true
并在 viewWillDisappear 上再次删除它:
override func viewWillDisappear(_ animated: Bool) {
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = false
}