Force portrait orientation while pushing from landscape View Controller

Tariq picture Tariq · Jan 31, 2013 · Viewed 19.3k times · Source

App Support: iOS6+

My app works in both portrait and landscape. But 1 controller should only works in a Portrait.

The problem is that when I am in landscape and push the view controller the new viewcontroller is in landscape as well until I rotate it to portrait. Then it's stuck in portrait as it should be.

Is it possible to always make it appear in portrait? Even if its parent is pushing it in landscape?

All the following code doesn't help

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

And this code works until and unless i am not pushing from landscape How to force a UIViewController to Portrait orientation in iOS 6

Answer

Tariq picture Tariq · Feb 1, 2013

I solved this by adding following lines in ViewDidLoad

UIViewController *c = [[UIViewController alloc]init];
[self presentViewController:c animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];