我有一个简单的应用程序,其中一个肖像 UINavigationController
呈现一个风景 UINavigationController
. 通常它工作正常 . 有时,横向视图控制器以纵向显示,但具有横向边界 .
以下是它的工作原理:为了限制导航控制器的方向,我有一个派生自 UINavigationController
的类 OrientableNavigationController
并公开了特定于旋转的属性:
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 }
}
}
我在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
}
它看起来像这样:
并且“现在的景观视图控制器”按钮已连接到这样做:
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)
}
大多数时候,这很好 . 横向视图控制器如下所示:
但有时会发生这种情况:
这在模拟器和设备中都会发生 . 有任何想法吗?