OGeek|极客世界-中国程序员成长平台

标题: ios - 使用 Swift 在 iOS 中全局检测屏幕转换(切换) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:01
标题: ios - 使用 Swift 在 iOS 中全局检测屏幕转换(切换)

我想知道是否有办法在应用程序中全局检测屏幕切换,例如来自应用程序代表?我知道有 isMovingFromParentViewController() 和 isMovingToParentViewController() 之类的方法,但这些方法只能在本地使用。任何信息都会有所帮助,谢谢!

克里斯蒂安



Best Answer-推荐答案


如果你打算使用全局 UINavigationController 作为 AppDelegateUIWindowrootViewController如下方式。

let navigationController:UINavigationController = UINavigationController(rootViewController: ViewController())
navigationController.delegate = self

window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()

那么 UINavigationControllerDelegate 中的以下委托(delegate)方法将完成 AppDelegate

中的工作
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {

}

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {

}

当新的UIViewController 出现在UINavigationController 中时,上述函数将帮助您。不幸的是,没有与 UIWindow 相关的方法来观察这些变化。如果有帮助,请告诉我。

关于ios - 使用 Swift 在 iOS 中全局检测屏幕转换(切换),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40532615/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4