Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting App in portrait even if device in landscape

My app is designed to work in Portrait and in landscape orientations. Starting with iOS 8 there is a problem starting the app if the device is held in landscape orientation. The UI is simply not correct initialized.

My Users have to close the app and start again while holding the device in portrait.

How can I arrange that the app is always launching in portrait mode without locking it to this mode? After this the user should be allowed to change to all four orientations.

In my view controller shouldAutorotate returns true, and supportedInterfaceOrientations returns all.

like image 619
thpitsch Avatar asked Nov 26 '22 06:11

thpitsch


1 Answers

Heres a solution that worked for me on ios 11, my app supports Portrait and landscape but the initial view has tab bar that must be in portrait always. Set orientation lock and create a Utility that you should be executed inside didFinishLaunchingWithOptions like this :

AppUtility.lockOrientation(.portrait)

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return self.orientationLock
}

struct AppUtility {
    static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
        if let orientationDelegate = UIApplication.shared.delegate as? AppDelegate {
            orientationDelegate.orientationLock = orientation
        }
    }
like image 88
Sergio Trejo Avatar answered Mar 31 '23 15:03

Sergio Trejo