0

In our app, there is one ViewController should only be available in Portrait mode, hence we've used the usual way of making sure that only this mode works:

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationPortraitUpsideDown;
}

But if the user is in landscape mode before, and then switches to this specific ViewController, it does not rotate to Portrait mode automatically. Hence, we were looking for a way to force Portrait mode and found this:

[[UIDevice currentDevice] setValue:
 [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                            forKey:@"orientation"];

It works fine, but since it's not an official way of doing things, we might get rejected now or in the future.

Is there a way to achieve the same result but with non-private APIs?

EDIT: I am specifically asking how to avoid using the solution posted in this question (How do I programmatically set device orientation in iOS7?), so how can it even possibly be a duplicate?

Community
  • 1
  • 1
keyboard
  • 2,137
  • 1
  • 20
  • 32
  • Possible duplicate of [How do I programmatically set device orientation in iOS7?](http://stackoverflow.com/questions/20987249/how-do-i-programmatically-set-device-orientation-in-ios7) – Greg Aug 15 '16 at 15:46
  • check this https://stackoverflow.com/questions/38308919/unable-to-force-uiviewcontroller-orientation/38308987#38308987, i can tell you that i have one application in the Appstore with this behavior and is perfectly accepted – Reinier Melian Aug 15 '16 at 16:03

2 Answers2

0

This help me.

In Your ViewController


override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscapeLeft // or .landscapeRight or As per your requirement 
}

override var shouldAutorotate: Bool {
    return true
}

In DidLoad method



let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.myOrientation = .landscape // or .landscapeRight or As per your requirement 

In App Delegates

 var myOrientation: UIInterfaceOrientationMask = .portrait

  func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
            return myOrientation
        }
-1

shouldautorotate method should return NO for that view controller. If this doesn't work then use post notifications to manage orientation.

Sabby
  • 403
  • 3
  • 15