iOS:supportedInterfaceOrientations()的轮换问题

我正在使用故事板在
swift中构建应用程序.它由UITabBarController,UINavigationController和threeUIViewController组成.

TabBarController – > NavigationController – > ViewController1 – > VC2 – > VC3

我想构建应用程序,以便前两个视图控制器只能在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在Portrait中被阻止,第三个而不是在LandscapeRight方向处于纵向模式.仅当我将设备旋转到LandscapeRight时,界面才会旋转到正确的位置.我怎么解决这个问题?

此外,当我尝试使用BackButton返回navigationBar的第二个ViewController时,页面仍然被阻止,并且我收到此错误:对< _TtC11AppNamem6Detail:0x145ab3a0>的开始/结束外观转换的不平衡调用.

最佳答案 在子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)
}
点赞