Reputation: 2146
I am trying to set the orientation for a UIViewController
and disallow all orientation changes on that UIViewController
. It is important to note that the app allows all orientations, but on this one view controller I want to allow only portrait orientation.
I am working on iOS 8, the app works with iOS 6 and up. The described behavior is happening in iOS 7 and 8. I cannot test it in iOS 6 so I am not sure if it occurs in iOS 6.
I have the following methods implemented:
- (BOOL)shouldAutorotate
{
return NO; // I've also tried returning YES - no success
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait; // I've also tried UIInterfaceOrientationPortrait - no success
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskPortrait; // Never called
}
I have been able to set the orientation for a view controller that is presented modally, but I am not presenting the view controller modally for this project. Can this even be done?
How do I specify the orientation for one UIViewController, without effecting the orientation of other UIViewControllers? Can this even be done?
Upvotes: 4
Views: 1885
Reputation: 4818
Supposing that your view controller is embedded in a navigation controller, you need to use a custom subclass of UINavigationController
and add:
- (BOOL)shouldAutorotate;
{
BOOL shouldAutorotate;
if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)])
{
shouldAutorotate = [self.topViewController shouldAutorotate];
}
else {
shouldAutorotate = [super shouldAutorotate];
}
return shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
NSUInteger supportedOrientations;
if ([[self topViewController] respondsToSelector:@selector(supportedInterfaceOrientations)]) {
supportedOrientations = [[self topViewController] supportedInterfaceOrientations];
}
else {
supportedOrientations = [super supportedInterfaceOrientations];
}
return supportedOrientations;
}
in your navigation controller. The methods in your view controller are just fine, so leave them as they are. That works in iOS 7-8 (haven't tested in iOS6)
Upvotes: 1