First of all, this isn't a duplicate. I've looked at all of the questions related to this on SO and none of them work for me. Hopefully it's just because I'm new to iOS development but I suspect this isn't possible in my case. I've attached a picture with the view controller circled that I want to disable rotation for.
I've already tried: Subclassing the view controller that I want to disable rotation for and using the shouldAutoRotate
method by setting it to NO. Apparently this doesn't work because it's the navigation controller that dictates whether its view controllers can rotate. So, I subclassed UINavigationController
and used this class in storyboard instead of the default navigation controller. In this class I've set shouldAutoRotate
to NO. It still doesn't work. Don't really know what I'm doing wrong.
When I extend the root view controller with my view controller class with shouldAutoRotate
set to NO, it disables rotation...for the whole app. This is not what I want. I only want the rotation to be disabled for the view controller circled in the picture.
Thanks in advance!
UINavigationController+Rotation Category
When u usign navigation controller
SupportedInterfaceOrientaions
ofViewController
will not work adding this category to ur Project will get the response from your viewController also instead ofNavigationController
alone.My case has 3 view controller:
- first view controller: portrait
- second view controller: landscape right (has navigation controller and was presented by first view controller)
- third view controller: portrait (has navigation controller and was pushed by second view controller )
And this is my solution in swift 3:
------------------------------------
At AppDelegate:
- Add this property to save your setting
-Then create a function to set rotate for your device:
- Finally, implement support orientation method:
--------------------------------
Then you can call like this before display destination view controller
For example, in my case: When user tap on button at the First to present the Second, I'll do like this
And the close button at the Second I'll do like this
That's all! You could never mind using navigation controller or not. Hope this helps you guys ^__^!
Add your AppDelegate.h
AppDelegate.m
In view Controller that you want to disable rotation
Thanks to @ozgur for the fix which worked for me. I would 'vote up', but apparently I'm not good enough (whatever!) to vote whether a fix works or not. Anyway, here it is in Swift:
In AppDelegate.swift:
In the ViewController that you want to block rotation, add UIApplicationDelegate to your class...
and then create a reference to the AppDelegate...
In viewDidLoad, set appDelegate.blockRotation = true:
In viewWillAppear, set the orientation to force the device to the chosen orientation (Portrait in this example):
Then in viewWillDisappear, or in prepareForSegue, set appDelegate.blockRotation = false:
This worked for me in this exact (multiple view controllers in a Navigation Controller) scenario, after many hours of reading other solutions on this site. Thanks again to @ozgur - I'd up-vote your answer if I could!
Happy trails!
For those of you using Swift 2, you can follow Andre's answer, but in step one, use the code below in your AppDelegate.swift:
supportedInterfaceOrientationsForWindow: now returns a UIInterfaceOrientationMask instead of an Int.
You have to check in your root view controller is rotation allowed in current top controller and return YES or NO in supportedInterfaceOrientations method. So it should be like the following code in your root controller (adapt the code to your case):
Then in each view controller add supported interface orientations, for example: