我在我的应用程序中添加了一个功能,这是在Swift 4中,它允许视图在显示图表时定向到横向或纵向。我为每个方向创建了两个独立的视图,并创建了处理该过程的逻辑。它工作正常,除了一个小问题,我可以解决,如果我能确定调用视图控制器。我试过用
self.window?.rootViewController?.presentedViewController
字符串但这并没有被证明是准确的。我已经检查了应用程序和窗口的参数没有任何成功。这是可能的还是我需要依赖一个全局变量来代替?
rdrgkggo1#
应用委托返回一个位掩码,该位掩码指示整个应用支持的方向的“最大集合”,而不管当前呈现的是哪个视图控制器。所呈现的具体视图控制器还被询问(supportedInterfaceOrientations)并返回其自己支持的方向。最后,两个返回值将相交以获得当前支持的方向。如果我没记错的话,这两种价值观至少必须在一个方向上一致。例如,如果应用程序代表说它只支持“纵向(向上)”,但视图控制器只支持“横向左”,则您的应用程序将崩溃(或做更糟糕的事情)。
supportedInterfaceOrientations
wb1gzix02#
你可以像这样检索应用的最上面的viewController:
func topViewController() -> UIViewController { let controller = UIApplication.shared.keyWindow?.rootViewController if let presentedVC = controller?.presentedViewController { return presentedVC } return controller! }
字符串
nzk0hqpo3#
进入topViewController取决于视图的配置。使用.isKind(of:VC)帮助您根据您可能使用的视图层次结构返回适当的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 }
3条答案
按热度按时间rdrgkggo1#
应用委托返回一个位掩码,该位掩码指示整个应用支持的方向的“最大集合”,而不管当前呈现的是哪个视图控制器。
所呈现的具体视图控制器还被询问(
supportedInterfaceOrientations
)并返回其自己支持的方向。最后,两个返回值将相交以获得当前支持的方向。
如果我没记错的话,这两种价值观至少必须在一个方向上一致。例如,如果应用程序代表说它只支持“纵向(向上)”,但视图控制器只支持“横向左”,则您的应用程序将崩溃(或做更糟糕的事情)。
wb1gzix02#
你可以像这样检索应用的最上面的viewController:
字符串
nzk0hqpo3#
进入topViewController取决于视图的配置。使用.isKind(of:VC)帮助您根据您可能使用的视图层次结构返回适当的ViewController:
字符串