2

這或多或少是我Main.storyboard情況:Swift:將自定義類分配給所有UINavigationControllers是否正確?

enter image description here

在那裏我有一個根UITabBarController 5點不可能性的選擇。然後,我想讓一些UIViewController可以旋轉到風景,而我還想讓其他一些UIViewController只有風景模式。所以我寫了這個file.swift:

class CustomNavigationController: UINavigationController, UINavigationControllerDelegate { 

    override func shouldAutorotate() -> Bool { 
    if (self.topViewController?.isKindOfClass(HomeViewController) != nil) {return false} 
    else if (self.topViewController?.isKindOfClass(ServicesViewController) != nil) {return false} 
    else if (self.topViewController?.isKindOfClass(PhotoGalleryViewController) != nil) {return false} 
    else if (self.topViewController?.isKindOfClass(SelectMapViewController) != nil) {return false} 
    else if (self.topViewController?.isKindOfClass(MapViewController) != nil) {return false} 
    else {return true} 
    } 

} 

class CustomTabBarController: UITabBarController, UITabBarControllerDelegate { 
    override func shouldAutorotate() -> Bool { 
    return (self.selectedViewController as! UINavigationController).shouldAutorotate() 
    } 
} 

,我已經分配給所有UINavigationController相同的S類 CustomNavigationController而我已經分配CustomTabBarControllerUITabBarController。 結果是沒有視圖控制器不旋轉。這是因爲我給他們分配了同一個班級嗎?我應該爲每個UINavigationController創建一個自定義導航控制器類嗎?

UPDATE

的部分解決方案,我發現,即使這是一個有點複雜,如下。我已經修改了以前的文件這樣的:

class CustomNavigationController: UINavigationController, UINavigationControllerDelegate { 

override func shouldAutorotate() -> Bool { 
    return (self.topViewController?.shouldAutorotate())! 
} 

} 

類CustomTabBarController:的UITabBarController,UITabBarControllerDelegate { 覆蓋FUNC shouldAutorotate() - >布爾{ 回報(self.selectedViewController爲UINavigationController的!).shouldAutorotate() } }

然後,在視圖控制器,其中旋轉被允許我只是有:

override func shouldAutorotate() -> Bool { 
    return true 
} 

而在視圖控制器,其中旋轉不允許我:

override func shouldAutorotate() -> Bool { 
    return false 
} 

override func viewWillAppear(animated: Bool) { 
    let value = UIInterfaceOrientation.Portrait.rawValue 
    UIDevice.currentDevice().setValue(value, forKey: "orientation") 
} 

反正有一個小問題,因爲它設置模式爲人像動畫是不正確的意思是,屏幕的寬度不作調整。如果我從橫向視圖控制器轉到僅縱向查看控制器,則視圖控制器框架不正確。我得到

enter image description here

,而不是這樣的:

enter image description here

回答

1

中的AppDelegate試試這個

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { 
    if let rootController = self.window?.rootViewController as? UITabBarController { 
     if let navigationController = rootController.selectedViewController as? UINavigationController { 
      let controller = navigationController.topViewController! 

      if controller.isKindOfClass(HomeViewController.classForCoder()) { 
       return UIInterfaceOrientationMask.Portrait 
      } 
      if controller.isKindOfClass(ServicesViewController.classForCoder()) { 
       return UIInterfaceOrientationMask.Portrait 
      } 
      if controller.isKindOfClass(PhotoGalleryViewController.classForCoder()) { 
       return UIInterfaceOrientationMask.Portrait 
      } 
      if controller.isKindOfClass(SelectMapViewController.classForCoder()) { 
       return UIInterfaceOrientationMask.Portrait 
      } 
      if controller.isKindOfClass(MapViewController.classForCoder()) { 
       return UIInterfaceOrientationMask.Portrait 
      } 
     } 
    } 
    return UIInterfaceOrientationMask.All 
} 

更新:

此方法強制應用程序來改變方向在ViewCo ntroller應該是隻在畫像(HomeViewController,ServicesViewController,PhotoGalleryViewController,SelectMapViewController,MapViewController中):

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 
    let value = UIInterfaceOrientation.Portrait.rawValue 
    UIDevice.currentDevice().setValue(value, forKey: "orientation") 
} 
+0

謝謝你的建議,但恐怕行不通......他們都與旋轉也不例外:( –

+0

@LoryLory對不起,我編輯了我的答案。以前只與Presented控制器一起工作。這一個應該工作。此外,即使沒有CustomNavigationController,它也可以工作。嘗試它;) –

+0

確定它的工作原理,但現在我有我的問題更新中描述的問題:如果我從一個橫向視圖控制器到肖像僅查看控制器然後視圖控制器框架不正確。選擇新的圖像。 –

相關問題