Ios 从纵向到横向的快速选择定向问题
环境:Ios 从纵向到横向的快速选择定向问题,ios,swift,orientation,landscape,portrait,Ios,Swift,Orientation,Landscape,Portrait,环境: 使用最新的Xcode 8.3 语言:Swift3 所有设备都已更新(iPhone 7 v10.3.1) (目标->部署信息->设备方向)对于“设备”的所有选项都设置为纵向 情景: 我有两个ViewController(VC1和VC2),它们都继承自UIViewController VC1始终意味着是纵向的 VC2始终是一种景观 VC1和VC2的所有子视图和对象都已配置,并已添加到viewDidLoad调用中的ViewController视图中 我通过呼叫从VC1演示VC2(因为我需
- 使用最新的Xcode 8.3
- 语言:Swift3
- 所有设备都已更新(iPhone 7 v10.3.1)
- (目标->部署信息->设备方向)对于“设备”的所有选项都设置为纵向
- 我有两个ViewController(VC1和VC2),它们都继承自UIViewController
- VC1始终意味着是纵向的
- VC2始终是一种景观
- VC1和VC2的所有子视图和对象都已配置,并已添加到viewDidLoad调用中的ViewController视图中
- 我通过呼叫从VC1演示VC2(因为我需要导航条):
- 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 }
- 这通常有效,但在某些情况下,视图会导致VC2垂直显示,属性从水平视图派生:
- 这是随机发生的,在Xcode(包括它的调试控制台)中没有显示错误
- 请注意,在后台线程上运行的任务需要使用GPU。这些是从除VC1和VC2之外的ViewController声明的
- 这可能会导致以错误的顺序调用对时间敏感的设备定向函数
- 我尝试了以下链接中的实现:
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
}