环境:
情景:
我通过电话从VC1呈现VC2(因为我需要一个导航栏):
let vc2 = UINavigationController(rootViewController: VC2())
self.present(vc2, animated: true, completion: nil)
VC2是继承自UINavigationController还是UIViewController对问题的发生没有影响。
AppDelegate.swift强制执行方向:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
if rootViewController is VC2 {
let vc2 = rootViewController as! VC2
if vc2.isPresented {
return .landscapeRight
}
}
return .portrait
}
上面的函数使用这个函数来查找topViewControllerWithRootViewController:
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
}
问题:
附加说明: