Is there a way to lock rotation until the view has loaded?
Yes there is. But before we proceed to that, since I saw your video/issue, usually when needed, a view must be re-layouted. Not sure about the term, but you can adjust its constraints based on the current orientation.
Anyways, for the way to lock the rotation until you want it be unlocked, I've found this utility quite long time ago.
import UIKit
struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
/// OPTIONAL Added method to adjust lock and rotate to the desired orientation
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}
}
This has been very helpful to me. Now to use it, it's no brainer.
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Lock immediately the rotation to portrait!
// Use lockOrientationAndRotateTo if it's needed.
AppUtility.lockOrientation(.portrait, andRotateTo: .portrait)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Then finally, after loading something, enable rotation.
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) {
// Assuming that the loading takes 5 seconds or so.
AppUtility.lockOrientation(.all)
}
}
}
Good luck! I hope this helps!