On iOS, is it possible to relaunch an app automatically when the device orientation changes? Android has this feature (just make sure android:configChanges does not include orientation).
There are no methods available in iOS to restart your application, BUT you could manually reinstantiate initial root UIViewController
when orientation changes.
To reinstantiate root UIViewController
you could use following static functions in your AppDelegate
class:
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()
}
To listen for orientation change use following method in UIViewController
:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
AppDelegate.resetViewController() /// "Restarts" application
}
While this is a viable approach, I would highly discourage you from restarting your application when orientation change happens and just handle orientation changes how it supposed to be handled according to Apple.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.