0

Application must be Portrait on startup, but after I can rotate it.

How do this, maybe programmatically?

Golovanov Dmytrii
  • 527
  • 1
  • 6
  • 20
  • What is the issue?? – dahiya_boy Feb 19 '18 at 10:29
  • Check out this Ans : https://stackoverflow.com/a/35285490/5094664 – Sid Mhatre Feb 19 '18 at 10:31
  • @SidMhatre OP states that app must support landscape orientation. He wants portait-only at startup. – lolbas Feb 19 '18 at 10:32
  • What does "load application" means to you? Usually, it is some splash image that hangs until application is good to go. Are you doing some other things? If so, specify them in question. – lolbas Feb 19 '18 at 10:33
  • @lolbas read suggested link question and answer. Its for Portrait mode only – Sid Mhatre Feb 19 '18 at 10:36
  • Possible duplicate : check info.plist. Make sure Supported Interface Orientations contains only one value(Portrait). https://stackoverflow.com/a/29791531/5094664 and https://stackoverflow.com/a/35285490/5094664 – Sid Mhatre Feb 19 '18 at 10:41

2 Answers2

1

You can add this to each controller that you would like to have in portrait mode:

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait //return the value as per the required orientation
}

Or you can set it in settings (as @Sid Mhatre already mentioned): https://stackoverflow.com/a/35285490/8382008

M. Wojcik
  • 2,301
  • 3
  • 23
  • 31
1

You could create a extension for the orientation in your appDelegate like this:

protocol MyAppDelegate: class {
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
}


extension AppDelegate: MyAppDelegate {
    func application(_ application: UIApplication,
                     supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return shouldRotate ? .allButUpsideDown : .portrait
    }
}

In your viewControllers where you want to allow rotation you should implement this inside the viewDidLoad method:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true

And also remove it again on viewWillDisappear:

override func viewWillDisappear(_ animated: Bool) {
    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.shouldRotate = false
}
Kingalione
  • 4,237
  • 6
  • 49
  • 84