How to change the device orientation programmatically in iOS 6

uttam picture uttam · Sep 29, 2012 · Viewed 76.6k times · Source

In iOS 5 we could change the device orientation programmatically like so:

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

But in iOS 6 setOrientation is deprecated, how may i change the device orientation programmatically in iOS 6?

Answer

Lukasz 'Severiaan' Grela picture Lukasz 'Severiaan' Grela · Jun 17, 2014

Here are my "five cents", tested on iOS7 with ARC

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

This doesnt generate "leak" warning as performSelector will.

UIAlertView - with this code, when you open UIAlertView during view(will/Did)appear you will notice that all but this view is in portrait (apple, really?) I wasn't able to force the view to reorient but found that if you put slight delay before opening the UIAlertView then view has time to change orientation.

Note I'm releasing my app week commencing 12/09/2014 and I will update post if it will pass or fail.