从纵向视图控制器呈现的景观视图控制器偶尔会出现在具有横向边界的纵向方向上

Mat*_*omi 5 landscape uiviewcontroller uinavigationcontroller ios swift

我有一个简单的应用程序,其中肖像UINavigationController呈现景观UINavigationController.通常它工作正常.有时,横向视图控制器以纵向显示,但具有横向边界.

以下是它的工作原理:为了限制导航控制器的方向,我有一个类,OrientableNavigationController它派生自UINavigationController并公开了特定于旋转的属性:

class OrientableNavigationController: UINavigationController {
  private var _supportedInterfaceOrientations: UIInterfaceOrientationMask = .all
  private var _shouldAutorotate: Bool = false
  private var _preferredInterfaceOrientationForPresentation: UIInterfaceOrientation = .portrait

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    get { return _supportedInterfaceOrientations }
    set { _supportedInterfaceOrientations = newValue }
  }

  override var shouldAutorotate: Bool {
    get { return _shouldAutorotate }
    set { _shouldAutorotate = newValue }
  }

  override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    get { return _preferredInterfaceOrientationForPresentation }
    set { _preferredInterfaceOrientationForPresentation = newValue }
  }
}
Run Code Online (Sandbox Code Playgroud)

我在AppDelegate中初始化其中一个,给它一个视图控制器并将其设置为rootViewController窗口的:

private var portraitNavigationController = OrientableNavigationController()

func application(
  _ application: UIApplication,
  didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  portraitNavigationController.supportedInterfaceOrientations = .portrait
  portraitNavigationController.shouldAutorotate = true
  portraitNavigationController.preferredInterfaceOrientationForPresentation = .portrait

  let portraitViewController = PortraitViewController()
  portraitViewController.delegate = self

  portraitNavigationController.pushViewController(portraitViewController, animated: false)

  window = UIWindow(frame: UIScreen.main.bounds)

  window!.rootViewController = portraitNavigationController

  window!.makeKeyAndVisible()

  return true
}
Run Code Online (Sandbox Code Playgroud)

它看起来像这样:

并且"现在的景观视图控制器"按钮已连接到这样做:

func portraitViewControllerButtonWasTouched(_ viewController: PortraitViewController) {
    let landscapeNavigationController = OrientableNavigationController()
    landscapeNavigationController.supportedInterfaceOrientations = .landscape
    landscapeNavigationController.shouldAutorotate = true
    landscapeNavigationController.preferredInterfaceOrientationForPresentation = .landscapeLeft
    landscapeNavigationController.isNavigationBarHidden = true

    let landscapeViewController = LandscapeViewController()

    landscapeNavigationController.pushViewController(landscapeViewController, animated: false)

    portraitNavigationController.present(landscapeNavigationController, animated: true)
  }
Run Code Online (Sandbox Code Playgroud)

大多数时候,这很好.横向视图控制器如下所示:

有时会发生这种情况:

这在模拟器和设备中都会发生.有任何想法吗?