Created
January 12, 2018 04:41
-
-
Save sunnyleeyun/7c853f8f0c8bdbd7369063e121019aff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { | |
if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) { | |
if (rootViewController.responds(to: Selector(("canRotate")))) { | |
// Unlock landscape view orientations for this view controller | |
return .allButUpsideDown; | |
} | |
} | |
// Only allow portrait (standard behaviour) | |
return .portrait; | |
} | |
private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? { | |
if (rootViewController == nil) { return nil } | |
if (rootViewController.isKind(of: UITabBarController.self)) { | |
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController) | |
} else if (rootViewController.isKind(of: UINavigationController.self)) { | |
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController) | |
} else if (rootViewController.presentedViewController != nil) { | |
return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController) | |
} | |
return rootViewController | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment