【发布时间】:2017-10-03 09:10:51
【问题描述】:
我有 2 个视图控制器,VC1 和 VC2。
VC1 目前以模态方式呈现 VC2。
VC1 唯一的方向应该是纵向,但 VC2 可以有所有方向。
问题是当我在 VC2 中旋转到横向模式然后关闭时,VC1 也处于横向模式!那不应该发生!
注意:没有导航控制器或UITabbarcontroller
我在下面添加我的代码。
Appdelagate:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
if (rootViewController.responds(to: Selector(("canRotate")))) {
// Unlock landscape view orientations for this view controller
return .allButUpsideDown
}
}
// Only allow portrait (standard behaviour)
return .portrait;
}
private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
if (rootViewController == nil) { return nil }
if (rootViewController.isKind(of: (UITabBarController).self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
} else if (rootViewController.isKind(of:(UINavigationController).self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
} else if (rootViewController.presentedViewController != nil) {
return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
}
return rootViewController
}
VC2中的代码:
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
}
func canRotate() -> Void {}
链接到我寻求帮助并找到此代码的地方 Website where I found Code
非常感谢您的帮助!
【问题讨论】:
-
你做错了。
supportedInterfaceOrientations的实现在哪里?两个视图控制器都必须有一个。 -
我目前在任何地方都没有,因为我尝试过它,但它似乎不适用于 iOS 10?你能详细说明一下吗。对此非常新!我添加了一个指向问题的链接,显示我在哪里找到此代码
-
supportedInterfaceOrientations自 iOS 6 左右以来一直是处理设备旋转的首选机制。您没有提到 VC1 是如何呈现的,但它似乎应该覆盖该方法来控制它的旋转。 -
您好,感谢您的帮助。现在它是我的测试应用程序的根视图控制器,但可能会改变。你能给我指出如何做到这一点的正确方向吗?或者请她一些代码?
-
仔细阅读 UIViewController 文档developer.apple.com/reference/uikit/…中标题“处理视图旋转”下的内容@
标签: ios iphone swift model-view-controller screen-orientation