我想将所有ViewController中的方向锁定为纵向,但其中之一除外,当将其始终压在landscapeRight中时。
我尝试了许多解决方案,使用了UINavigationController扩展名,覆盖了supportedInterfaceOrientationsshouldAutorotate,但是没有运气。

最佳答案

我已经找到了解决方案,目前它正在运行。

  • 在每个视图控制器上,您应该放置以下代码以仅支持所需的旋转(在示例中为landscapeRight):
    override var shouldAutorotate: Bool {
        return true
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.landscapeRight
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .landscapeRight
    }
    

    在另一种实现相同的方法,但具有纵向方向。
  • 创建UINavigationController的扩展名
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return (self.topViewController?.supportedInterfaceOrientations)!
    }
    
    open override var shouldAutorotate: Bool {
        return true
    }
    

  • 注意:还要在Project的目标中启用所有需要的支持
    方向。

    我想以横向模式展示的唯一一个控制器以模态展示。

    10-06 13:04
    查看更多