iOS:supportedInterfaceOrientations()的轮换问题

时间:2014-08-13 10:21:17

标签: ios swift uitabbarcontroller uinavigationbar orientation

我正在使用故事板在swift中构建应用程序。它由UITabBarControllerUINavigationController and three 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)
        }
   }

自定义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在Portrait中被阻止,第三个而不是在LandscapeRight方向处于纵向模式。仅当我将设备旋转到LandscapeRight时,界面才会旋转到正确的位置。我该如何解决这个问题?

此外,当我尝试使用BackButton返回navigationBar的第二个ViewController时,页面仍然被阻止,我收到此错误Unbalanced calls to begin/end appearance transitions for <_TtC11AppNamem6Detail: 0x145ab3a0>.

3 个答案:

答案 0 :(得分:1)

在子viewController中,您可以指定要支持的方向:

override func supportedInterfaceOrientations() -> Int
{
    // designate we only like to be in landscape mode
    return UIInterfaceOrientationMask.Landscape.rawValue.hashValue
}

在navigationController(root)中,指定它是否可以根据子视图控制器进行旋转:

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 :(得分:1)

Swift 3需要这些是var而不是func类型。

override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .all
}

答案 2 :(得分:0)

根据对同一问题的大量研究。某些资源声明所有视图控制器必须是相同的模式。使用UINavigationController时,您不能与其他人有所不同。