[英]iOS - Relaunch app on orientation change?
在 iOS 上,是否可以在设备方向更改时自动重新启动应用程序? Android 具有此功能(只需确保 android:configChanges 不包含方向)。
iOS 中没有可用的方法来重新启动您的应用程序,但是您可以在方向更改时手动重新实例化初始根UIViewController
。
要重新实例化 root UIViewController
您可以在AppDelegate
类中使用以下静态函数:
static func getWindow() -> UIWindow? {
return UIApplication.shared.keyWindow
}
static func resetViewController() {
let window = getWindow()
let controller = ... /// Instantiate your inital `UIViewController`
window?.rootViewController = controller
window?.makeKeyAndVisible()
}
要侦听方向更改,请在UIViewController
使用以下方法:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
AppDelegate.resetViewController() /// "Restarts" application
}
虽然这是一种可行的方法,但我强烈建议您不要在发生方向更改时重新启动应用程序,而只处理方向更改,根据 Apple 的规定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.