Here's my view controller code for the view controller that I only want to display in portrait orientation…
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
- (BOOL)shouldAutorotate {
return NO;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
and here's how I'm presenting it…
MyViewController *myVC = [[MyViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:myVC];
[self presentViewController:nav animated:YES completion:nil];
If the device is in landscape orientation and the view controller is presented, it correctly autorotates to portrait orientation. But if you rotate the device after it's displayed, it autorotates to whatever orientation the device is in. I want to prevent autorotation after its displayed and force only portrait orientation for just this one view controller. This is an iOS8-only app.
Thanks in advance for your wisdom!
Best Answer
The problem is that you are setting the
supportedInterfaceOrientations
for the wrong object. You have implementedsupportedInterfaceOrientations
in MyViewController. But look at your code:nav
is the presented view controller - not MyViewController. Sonav
, the UINavigationController, governs whether we will rotate - not MyViewController.So, give
nav
a delegate in which you implementnavigationControllerSupportedInterfaceOrientations:
- and all will be well.