UIViewController UINavigationController rotation fix
Sometimes, we want to fix orientation(portrait or landscape).
Actually, we can set in XCode top.
You can check global setting.
How about each ViewController? How about program?
We sometimes set orientation to each ViewController.
ViewController
This is sample of fixed orientation(portrait)
@interface FixOrientationViewController () @end @implementation FixOrientationViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self.view setBackgroundColor:[UIColor whiteColor]]; } #pragma mark - Orientation - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // iOS5 return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(BOOL) shouldAutorotate { return YES; } - (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } @end
Key part is shouldAutorotateToInterfaceOrientation(deprecated), shouldAutorotate, preferredInterfaceOrientationForPresentation, supportedInterfaceOrientations.
These are settings for portrait and rotation
UINavigationController
Next is UINavigationController.
If you use UINavigationController directly, child ViewController setting doesn’t work.
FixOrientationViewController *controller = [[FixOrientationViewController alloc] init]; [controller setTitle:@"Bee"]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller]; [self presentViewController:nav animated:YES completion:nil]; // FixOrientationViewController setting doesn't work
In this case, UINavigationController isn’t portrait oriented.
So, FixOrientationViewController became root controller and should be portrait, but it doesn’t.
Fix Orientation UINavigationController
One solution is to extend class.
FixNavViewController.m
@interface FixNavViewController () @end @implementation FixNavViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } #pragma mark - Orientation - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // iOS5 return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(BOOL) shouldAutorotate { return YES; } - (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
Change Navigation codes
FixOrientationViewController *controller = [[FixOrientationViewController alloc] init]; [controller setTitle:@"Bee"]; FixNavViewController *nav = [[FixNavViewController alloc] initWithRootViewController:controller]; [self presentViewController:nav animated:YES completion:nil];