iOS:supportedInterfaceOrientations()存在旋转问题

4
我正在使用Storyboard开发一个Swift应用程序,它由一个UITabBarController、一个UINavigationController和三个UIViewController组成。
TabBarController -> NavigationController -> ViewController1 -> VC2 -> VC3
我希望构建应用程序时,前两个视图控制器只能以纵向方式显示,而第三个只能以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)
        }
   }

在自定义的视图控制器中,我添加了以下方法:

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)
}

当我启动应用程序时,前两个视图控制器被锁定在纵向模式下,而第三个视图控制器不是横向模式,而是纵向模式。只有当我将设备旋转到横向模式时,界面才会旋转到正确的位置。我该如何解决这个问题?
此外,当我尝试使用返回按钮返回导航栏的第二个视图控制器时,页面仍然被锁定,并且我收到以下错误:Unbalanced calls to begin/end appearance transitions for <_TtC11AppNamem6Detail: 0x145ab3a0>。

你的UITabBarController是否嵌入在UINavigationController中? - Razvan
3个回答

2
在子视图控制器中,您可以指定想要支持的方向:
override func supportedInterfaceOrientations() -> Int
{
    // designate we only like to be in landscape mode
    return UIInterfaceOrientationMask.Landscape.rawValue.hashValue
}

在导航控制器(根视图)中,您可以根据子视图控制器指定其是否可以旋转:
override func shouldAutorotate() -> Bool 
{
    return self.topViewController.shouldAutorotate()
}

然后在子视图控制器中,您设置是否应旋转:

override func shouldAutorotate() -> Bool
{
    // tells the root view controller we don't want to be reoriented
    return false
}

这将阻止所需视图旋转。
然后,如果您想强制将其定位到特定方向,则设置...
override func viewWillAppear(animated: Bool)
{
    // when the view controller appears rotate it to landscape mode
    UIApplication.sharedApplication().setStatusBarOrientation(UIInterfaceOrientation.LandscapeRight, animated: true)
}

1

Swift 3要求这些类型必须是var而不是func。

override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .all
}

0
根据对此问题的许多研究,一些资源指出所有视图控制器必须是相同的模式。在使用UINavigationController时,不能有一个与其他视图控制器不同。

你能发布那些资源的URL或链接吗? - jailani
如果堆栈/根窗口中的任何控制器 > 导航控制器 > 视图控制器在被询问是否可以旋转时返回“否”,那么设备将不会旋转。您仍然可以通过更换根视图控制器来间歇性地混合它。 - johndpope

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接