I have an app where I would like to support device rotation in certain views but other don't particularly make sense in Landscape mode, so as I swapping the views out I would like to force the rotation to be set to portrait.
There is an undocumented property setter on UIDevice that does the trick but obviously generates a compiler warning and could disappear with a future revision of the SDK.
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
Are there any documented ways to force the orientation?
Update: I thought I would provide an example as I am not looking for shouldAutorotateToInterfaceOrientation as I have already implemented that.
I want my app to support landscape and portrait in View 1 but only portrait in View 2. I have already implemented shouldAutorotateToInterfaceOrientation for all views but if the user is in landscape mode in View 1 and then switches to View 2, I want to force the phone to rotate back to Portrait.
I don't think this is possible to do at run-time, though you could of course just apply a 90 degree transform to your UI.
This works for me (thank you Henry Cooke):
The aim for me was to deal with landscape orientations changes only.
init method:
FWIW, here's my implementation of manually setting orientation (to go in your app's root view controller, natch):
coupled with the following
UINavigationControllerDelegate
method (assuming you're using aUINavigationController
):That takes care of rotating the root view according to whether an incoming
UIViewController
supports the current device orientation. Finally, you'll want to hook uprotateInterfaceToOrientation
to actual device orientation changes in order to mimic standard iOS functionality. Add this event handler to the same root view controller:Finally, register for
UIDeviceOrientationDidChangeNotification
notifications ininit
orloadview
like so:If you are using UIViewControllers, there is this method:
Return
NO
for the view controllers containing the views you don't want to rotate.More info here
I solved this quite easily in the end. I tried every suggestion above and still came up short, so this was my solution:
In the ViewController that needs to remain Landscape (Left or Right), I listen for orientation changes:
Then in didRotate:
Keep in mind any Super Views/Subviews that use autoresizing, as the view.bounds/frame are being reset explicitly...
The only caveat to this method for keeping the view Landscape, is the inherent animation switching between orientations that has to occur, when it would be better to have it appear to have no change.
I've been digging and digging looking for a good solution to this. Found this blog post that does the trick: remove your outermost view from the key
UIWindow
and add it again, the system will then re-query theshouldAutorotateToInterfaceOrientation:
methods from your viewcontrollers, enforcing the correct orientation to be applied. See it : iphone forcing uiview to reorientate