我有一个带有几十个UIViewControllers的应用程序。 该应用程序仅被声明为纵向,因此在旋转屏幕时不会旋转。 我希望允许一个特定的视图旋转。
尝试添加以下内容
open override var shouldAutorotate: Bool {
get {
return true
}
}
override internal func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
let orientation: UIInterfaceOrientationMask = [UIInterfaceOrientationMask.portrait, UIInterfaceOrientationMask.landscape]
return orientation
}
override internal func shouldAutorotate() -> Bool {
return true;
}
没用。 任何帮助将不胜感激
答案 0 :(得分:1)
该应用宣布为肖像
那是你问题的一部分。如果应用程序仅允许纵向,则各个视图控制器所说的内容无关紧要 - 应用程序获胜。
该应用程序的工作是声明应用永远允许承担的每个方向。然后,各个视图控制器(在视图控件层次结构的顶层或其指定的子级或委托)可以声明这些方向的子集。
答案 1 :(得分:0)
您还需要实现appDelegates supportedInterfaceOrientations:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
我会做的是向AppDelegate添加一个局部变量,它保留当前允许的方向并将其返回到此处:
var allowedOrientations : UIInterfaceOrientationMask = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
{
return self.allowedOrientations
}
当您的特定屏幕打开时,请使用新支持的方向更新AppDelegate的支持。
此外,如果您的视图位于UINavigationController内,您可能需要继承UINavigationController并覆盖这些方法,并且只需获取您在app delegate中拥有的内容:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask
{
// TODO: Set a new orientation if needed
// Return the newly set orientation
if let delegate = UIApplication.shared.delegate as? AppDelegate
{
return delegate.allowedOrientations
}
return UIInterfaceOrientationMask.portrait
}
override var shouldAutorotate: Bool
{
return (Whatever the condition is for orientation)
}