Taz Taz - 1 year ago 147
iOS Question

Force iOS orientation to landscape on one modal view controller

I have a universal application that supports both iPad and iPhone. On the iPad I support all orientations and on the iPhone, only Portrait.

I want one view controller (which is displayed modally), when running on the iPhone, to be displayed in any orientation that the device is in. I have seen many tutorials and SO posts that suggest using the

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
app delegate method and this seems to work absolutely fine in respect to rotating the modal view controller. However, when I dismiss the modal view whilst in landscape, the entire app remains landscape.

Ideally, as soon as the modal view is dismissed the app should go back into portrait mode.

After investigation, it appears that the
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
callback is not called after dismissing the view controller and I can't figure out why.

I have had a play with the example app here, and the callback is fired when dismissing the view controller, which I can't figure out as to why. The only difference I can see is that my view hierarchy is a lot more complex and that I'm displaying a Navigation Controller rather than an explicit view controller but I don't see how that should affect the callback.

Any ideas as to finding a solution to this?


Answer Source

You can try to use this method (working in app store build):

[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
                               withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download