2017-08-24 81 views
0

我正在研究iOS應用程序,其中所有ViewController僅支持縱向方向,除了一個支持縱向和橫向的ViewController。我試試這個代碼的那個ViewController支持ios,swift中特定視圖控制器的橫向和縱向設備方向3

override var shouldAutorotate: Bool { 
    return false 
} 

override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 
    return .landscapeLeft 
} 

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { 
    return .landscapeLeft 
} 

但這沒有工作,任何人都可以幫助嗎? 感謝

回答

0

在AppDelegate中添加此事件

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 


    if let navigationController = UIApplication.topViewController() { 

     if navigationController is YOURVIEWCONTROLLERNAME { 
      return UIInterfaceOrientationMask.all 
     } 

     else { 
      return UIInterfaceOrientationMask.portrait 
     } 
    } 

    return UIInterfaceOrientationMask.portrait 
} 

extension UIApplication { 
class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? { 
    if let nav = base as? UINavigationController { 
     return topViewController(base: nav.visibleViewController) 
    } 
    if let tab = base as? UITabBarController { 
     if let selected = tab.selectedViewController { 
      return topViewController(base: selected) 
     } 
    } 
    if let presented = base?.presentedViewController { 
     return topViewController(base: presented) 
    } 
    return base 
} 

變化YOURVIEWCONTROLLERNAME到您的視圖控制器

+0

它的作品,非常感謝的人 – DevMhd

相關問題