我的应用程序仅处于纵向模式,但一些 View Controller (用于阅读 pdf)可以选择处于横向模式。 我正在使用此功能来允许横向模式
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;
}
然后在横向模式下,如果我们按后退按钮,我会使用此功能强制之前的 View Controller 返回纵向模式
override func viewWillDisappear(_ animated : Bool) {
super.viewWillDisappear(animated)
if (self.isMovingFromParentViewController) {
UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
}
}
它在 iPhone(plus)上运行良好,上一页返回到纵向模式,标签栏也很好,但有一个错误,仅出现在 iPhone no plus(分辨率较小)上,上一页返回在纵向模式下,但选项卡栏出现故障。
有人有想法吗?我不知道发生了什么..
最佳答案
我通过删除“解决”了该问题
override func viewWillDisappear(_ animated : Bool) {
super.viewWillDisappear(animated)
if (self.isMovingFromParentViewController) {
UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
}
在 subview 中
并将其添加到父 View 中。
override func viewDidAppear(_ animated: Bool) {
UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
}
我仍然不明白这个错误,但至少有一个解决方法......
请注意,如果我将此代码放入 viewWillAppear 中,我会遇到相同的错误
关于iOS swift 标签栏的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52996223/