I have a navigation controller which have a few view controllers. I need to support all orientations for all view controllers except one special view controller which only supports landscape. This special view controller appears in the middle of the navigation stack. I have done quite a lot of research but couldn't find any good solution. Here are the links that I have read and tried.
How to rotate screen to landscape?
How to autorotate from portrait to landscape mode? iPhone - allow landscape orientation on just one viewcontroller http://goodliffe.blogspot.com/2009/12/iphone-forcing-uiview-to-reorientate.html
Next I am going to try to replace navigation controller with presentModalViewController
in order to display the special view controller. Then I am going to create a new navigation view controller inside the special view controller to push the subsequent view controllers.
If anyone has a better idea, please let me know. Really appreciated!
UPDATE: I have successfully use the method I described above: replace pushViewController
with presentModalViewController
and create a new navigation controller.
You can make actions: Change Your code with accordance of schellsan suggestion, next - Try to add currentViewController(which will push to navigation viewController) as property to appDelegate. When You attempt to push view controller, set it to current view controller before this. Next - make a subclass of rootViewController in navigation controller. In this subclass owerload method
It should works if You not using a navigation bar and pushes new controller after popping an old
Every view controller pushed onto the navigation controllers stack have to support the same orientations. This means that it is not possible to have some view controllers only supporting portrait and others only supporting landscape. In other words all view controllers on the same navigation controller stack should return the same in the delegate:
But there is a simple solution to this! Here is an example for going from portrait to landscape. Here is the steps to do it and below is code to support it.
UINavigationController
, add an instance of the ‘fake’ view controller as root and an instance of your landscape view controller as second view controllerUINavigationController
instance as modal from the parent view controllerFirst, create a new view controller (FakeRootViewController) with this code:
Here is the code to present the view controller that you wish to show in landscape mode:
Remember that the landscapeViewController should also have this implementation:
It should be as simple as implementing
in each
UIViewController
pushed into yourUINavigationController
. In the case that oneUIViewController
's view shouldn't rotate,return NO
for that specific orientation in that specificUIViewController
. There's a gotcha here though, if yourUINavigationController
implementsit will block its viewControllers from receiving that method. In that case, you should forward the message to the topViewController using
There's a private API to force an orientation change. Put in your pushed view controller's
-viewWillAppear:
:To suppress the compiler warning, add this to the .m file of your view controller:
As always, there's a risk of being rejected and a risk of breaking in future OS versions when using private APIs. Do at your own risk!
Generally, presenting a modal view controller is the better solution in most cases.
You could try this code in your
UINavigationController
to call the current visible view'sshouldAutorotateToInterfaceOrientation
. In my case I have theUINavigationController
in aUITabBarController
but you could probably adapt it to other cases.