Reputation: 2355
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?
Upvotes: 0
Views: 706
Reputation: 173
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
}
Upvotes: 0
Reputation: 403
shouldautorotate method should return NO for that view controller. If this doesn't work then use post notifications to manage orientation.
Upvotes: -1