0

I want to keep a portrait orientation even if user rotate the phone. At the same time, I want to change the orientation with a button. If I put only portrait as supported orientation in plist and then rotate, application gives me error. If I put all the supported orientation but shouldAutorotate method to NO, Application crashes. So, basically as I can see I can only support multiple orientation if I let the application to autorotate.

Can I achieve what I need?

1
  • what kinda error are you talking about?
    – holex
    Apr 11, 2018 at 13:37

2 Answers 2

1
shouldAutorotate

this is a get-only property.

what you can do is:

override var shouldAutorotate: Bool {
    return false
}

Maybe this is also interesting for you: how to dissable and enable auto rotate on swift?

1
  • 2018-01-31 09:24:04.602401+0100 OnTheGo[11399:587160] *** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [UnityLandscapeRightOnlyViewController shouldAutorotate] is returning YES' Jan 31, 2018 at 8:27
-1

May be the following line of code will work for you. set orientation programmatically.

  1. In your Appdelegate class write this code:

    var orientationLock = UIInterfaceOrientationMask.all

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

2.Make custom helper class as follows:

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")
    }

}
  1. then use it in your desire viewcontroller like:

    override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated)

    AppUtility.lockOrientation(.portrait)
    // Or to rotate and lock
    // AppUtility.lockOrientation(.portrait, andRotateTo: .portrait)
    

    }

    override func viewWillDisappear(_ animated: Bool){ super.viewWillDisappear(animated)

    // Don't forget to reset when view is being removed
    AppUtility.lockOrientation(.all)
    

    }

note: keep remember Require Full Screen option is checked from target settings.

4
  • Hello @MRizwan33, why full screen option must be checked? Feb 2, 2018 at 9:30
  • bcz its split the screen. if u wanna split screen then leave it uncheck.
    – MRizwan33
    Feb 2, 2018 at 10:42
  • on ipad device oritation doesn't impact. if u want keep full screen than u need to check this for ipad.
    – MRizwan33
    Feb 2, 2018 at 10:53
  • i just need to change programmatically the orientation WITHOUT the autorotation. Feb 2, 2018 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.