我正在使用 Storyboard快速构建应用程序。它由 UITabBarController
组成, 一个 UINavigationController and three
UIViewController`。
TabBarController -> NavigationController -> ViewController1 -> VC2 -> VC3
我想构建应用程序,以便第一个 2 个 View Controller 只能在 Portrait 中,第三个只能在 LandscapeRight 中。
我为 UITabBarController 创建了一个子类:
class OrientationTab: UITabBarController {
override func shouldAutorotate() -> Bool{
if self.selectedViewController != nil{
if self.selectedViewController.respondsToSelector("shouldAutorotate") {
println("TAB - shouldAutorotate - inside if")
return self.selectedViewController.shouldAutorotate()
}
}
println("TAB - shouldAutorotate - outside if")
return true
}
override func supportedInterfaceOrientations() -> Int{
if self.selectedViewController != nil{
if self.selectedViewController.respondsToSelector("supportedInterfaceOrientations"){
println("TAB - supportedInterfaceOrientations - inside if")
return self.selectedViewController.supportedInterfaceOrientations()
}
}
println("TAB - supportedInterfaceOrientations - outside if")
return Int(UIInterfaceOrientationMask.All.rawValue)
}
对于 UINavigationController:
class OrientationNav: UINavigationController {
override func shouldAutorotate() -> Bool{
if self.topViewController.respondsToSelector("shouldAutorotate") {
println("NAV - shouldAutorotate - if")
return self.topViewController.shouldAutorotate()
}else{
println("NAV - shouldAutorotate - else")
return true
}
}
override func supportedInterfaceOrientations() -> Int{
if self.topViewController.respondsToSelector("supportedInterfaceOrientations"){
println("NAV - supportedInterfaceOrientations - if")
return self.topViewController.supportedInterfaceOrientations()
}else{
println("NAV - supportedInterfaceOrientations - else")
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
在自定义 ViewController 中,我添加了以下方法:
override func shouldAutorotate() -> Bool{
// This method is the same for all the three custom ViewController
return true
}
override func supportedInterfaceOrientations() -> Int{
// Portrait for the first 2 ViewController
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
// LandscapeRight for the third
return Int(UIInterfaceOrientationMask.LandscapeRight.rawValue)
}
当我启动应用程序时,前两个 ViewController 在纵向模式下被阻止,第三个不是在 LandscapeRight 方向而是在纵向模式下。只有当我将设备旋转到 LandscapeRight 时,界面才会旋转到正确的位置。我该如何解决这个问题?
此外,当我尝试使用 BackButton 返回导航栏的第二个 ViewController 时,页面仍然被阻止并且我收到此错误:Unbalanced calls to begin/end appearance transitions for <_TtC11AppNamem6Detail: 0x145ab3a0>.
最佳答案
在 subview Controller 中,您指定要支持的方向:
override func supportedInterfaceOrientations() -> Int
{
// designate we only like to be in landscape mode
return UIInterfaceOrientationMask.Landscape.rawValue.hashValue
}
在 navigationController(root)中指定它是否可以基于 subview Controller 旋转:
override func shouldAutorotate() -> Bool
{
return self.topViewController.shouldAutorotate()
}
然后在 subview Controller 中设置它是否应该旋转:
override func shouldAutorotate() -> Bool
{
// tells the root view controller we don't want to be reoriented
return false
}
这将停止旋转所需的 View 。
然后如果你想强制它进入一个特定的方向然后你设置...
override func viewWillAppear(animated: Bool)
{
// when the view controller appears rotate it to landscape mode
UIApplication.sharedApplication().setStatusBarOrientation(UIInterfaceOrientation.LandscapeRight, animated: true)
}
关于iOS:supportedInterfaceOrientations() 的旋转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25283673/