I have an app with 9-10 screens. I embedded a UINavigationController
into my view controller. I have few view controllers which I want set only portrait orientation: it means that rotating the device should not rotate these view controllers to landscape mode. I have tried the following solutions:
first:
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
but screen still rotates to landscape.
Second:
I created a custom view controller class as PortraitViewController
and added the code below in PortraitViewController.m
@interface PortraitViewController ()
@end
@implementation PortraitViewController
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
//Here check class name and then return type of orientation
return UIInterfaceOrientationMaskPortrait;
}
@end
After that I implemented PortraitViewController.h
as a base class
#import <UIKit/UIKit.h>
#import "PortraitViewController.h"
@interface Login : PortraitViewController
@end
It does not work at all, still allows view controller to rotate in landscape mode.
Is there any other solution i am using iOS 8 & don't want viewcontroller to rotate in landscape mode?
EDIT: Is it possible to have Landscape orientation only for some view controllers, and force other view controllers orientation to stick to Portrait?
Try to subclass the
UINavigationController
you are using because the defaultUINavigationController
is not forwarding theshouldAutorotate
method to you viewcontroller.Implement the following method in your
UINavigationController
subclassNow the
UINavigationController
forwards the method call to its current visibleUIViewController
so you need to implementshouldAutorotate
there individually to get your desired effect.If you want to temporarily disable automatic rotation, avoid manipulating the orientation masks to do this. Instead, override the shouldAutorotate method on the initial view controller. This method is called before performing any autorotation. If it returns NO, then the rotation is suppressed.
So you need to subclass 'UINavigationController', implement shouldAutorotate and use your navigation controller class in your storyboard.
}
an alternate approach can be found on
http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/
Create a category on the
UINavigationController
and overridesupportedInterfaceOrientations
When you Embedded
UINavigationController
, Containers don't ask their children whether to rotate or notTr this in your view controller
1) Make a bool variable in your AppDelegate.
2) Then copy & paste below code in AppDelegate.m (modify it as per your requirement)
3) Now copy & paste below code in the viewWillAppear method of the view controller for which you want to force orientation.
4) Now before pushing this view controller, you need to set the bool first. So, let's say you want to push ABC view controller
Hope this helps!!
You have to do it manually. All view controllers, in those you do not want to rotate view, implement following method.