2

嗨我想停止在我的项目中的视图控制器之一的自动旋转。在下面的快照, enter image description here禁用UINavigationController和UIViewController在Swift 4中的屏幕方向

我有一个开始UINavigationController,然后UIViewController。我已经实现下面的代码以停止自转:

extension UINavigationController { 

    override open var shouldAutorotate: Bool { 
     get { 
      return false 
     } 
    } 

    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{ 
     get { 
      return UIInterfaceOrientationMask.landscape 
     } 
    }} 

class ViewController: UIViewController { 

    @IBOutlet weak var imageView: UIImageView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override open var shouldAutorotate: Bool { 
     return false 
    } 

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask 
    { 
     return .landscape 
    } 
    } 

但上面的代码似乎并没有打电话,没有服用任何影响。我正在使用Xcode 9swift 4.0。 任何建议,将不胜感激。

问候, NEENA

+0

你改变你的'Info.plist'添加'UISupportedInterfaceOrientations'? –

+0

如果我将更改info.plist,那么它将为整个应用程序,我猜想,但我希望在某些视图控制器中的这种行为不是遍布整个应用程序。 – neena

+0

'UISupportedInterfaceOrientations':指定应用程序支持的界面方向。 –

回答

1

创建一个类并将其设置为UINavigationController,然后在你的故事板使用这个新的类/ XIB

在您的分机:

class NavigationController: UINavigationController {  
    override var shouldAutorotate: Bool { 
     return topViewController?.shouldAutorotate ?? super.shouldAutorotate 
    } 

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 
     return topViewController?.supportedInterfaceOrientations ?? super.supportedInterfaceOrientations 
    } 
} 

在你的控制器:

override var shouldAutorotate: Bool { 
    return false 
} 

override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 
    return .portrait 
} 
+0

它不工作:(。我有所有这些代码,当我旋转我的设备改变它的方向。 – neena

+0

请参阅编辑 – TagTaco

+0

我不知道发生了什么事情,即使编辑的版本也无法正常工作。试着去调试它,这个断点从来没有出现在这两个属性上 – neena

0

我是有同样的问题。我查看了所有堆栈溢出,并尝试了许多解决方案,但都没有工作。我在苹果开发者论坛上发现了这个解决方案,它的功能就像一个魅力。

我在这里添加他们的解决方案,希望其他人在未来能够更容易地找到,给author以及链接到their post

添加到您的AppDelegate文件:

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

    return self.orientationLock 

} 

struct AppUtility { 

    static func lockOrientation(_ orientation: UIInterfaceOrientationMask) { 

     if let delegate = UIApplication.shared.delegate as? AppDelegate { 

      delegate.orientationLock = orientation 

     } 

    } 

    static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) { 

     self.lockOrientation(orientation) 

     UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation") 

    } 

} 

添加到您要强制定向视图 - 控制:

override func viewDidAppear(_ animated: Bool) { 

    super.viewDidAppear(animated) 

    AppDelegate.AppUtility.lockOrientation(.portrait) 

} 

override func viewWillDisappear(_ animated: Bool) { 

    super.viewWillDisappear(animated) 

    AppDelegate.AppUtility.lockOrientation(.all) 

} 
+0

感谢您的回复。我测试了您的解决方案,但问题仍然存在。它在iPad上无法正常工作,正如我在前面提到的那样,如果“显示全屏”被选中,它将只能在iPad上运行,如果你知道任何适用于iPad的扩展解决方案,把这个标记打勾然后让我知道。 – neena