iOS 6.0 introduces new rotation methods and doesn't call shouldAutorotateToInterfaceOrientation:
.
You need to implement these methods to support both pre-iOS 6.0 and iOS 6.0+ :
// Do as many checks as you want to allow for other orientations here for pre-iOS 6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
// You can return YES here as this still checks supportedInterfaceOrientations
// before rotating, or you can return NO to 'lock' the view in whatever it's in...
// Making sure to return the appropriate value within supportedInterfaceOrientations
- (BOOL)shouldAutorotate
{
return YES;
}
// return supported orientations, bitwised OR-ed together
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
EDIT- If your UIViewController
is within a UINavigationController
's hierarchy
If your view controller is within a UINavigationController
's view hierarchy, the rotation methods are actually called on the navigation controller (not the view controller)... this was implemented funny by Apple in my opinion, but here's what you can do to allow the view controller to respond to these methods instead-
Create a category on UINavigationController
with the methods as follows:
// UINavigationController+Additions.h file
@interface UINavigationController (Additions)
@end
// UINavigationController+Additions.m file
#import "UINavigationController+Additions.h"
@implementation UINavigationController (Additions)
- (BOOL)shouldAutorotate
{
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
UIViewController *viewController = [self topViewController];
if ([viewController respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)])
{
return [viewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
- (NSUInteger)supportedInterfaceOrientations
{
UIViewController *viewController = [self topViewController];
if ([viewController respondsToSelector:@selector(supportedInterfaceOrientations)])
{
return [viewController supportedInterfaceOrientations];
}
return [super supportedInterfaceOrientations];
}
@end
EDIT 2 - If your UIViewController
is within a UITabBarController
's tabs
There's the same issue if your view controller is within a UITabBarController
's tabs.
once again, create a category on UITabBarController
to allow the view controller to respond instead:
// UITabBarController+Additions.h file
@interface UITabBarController (Additions)
@end
// UITabBarController+Additions.m file
#import "UITabBarController+Additions.h"
@implementation UITabBarController (Additions)
- (BOOL)shouldAutorotate
{
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
UIViewController *selectedViewController = [self selectedViewController];
if ([selectedViewController respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)])
{
return [selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
- (NSUInteger)supportedInterfaceOrientations
{
UIViewController *selectedViewController = [self selectedViewController];
if ([selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
{
return [selectedViewController supportedInterfaceOrientations];
}
return [super supportedInterfaceOrientations];
}
@end
EDIT 3
Here's also some links about Objective-C Categories:
http://macdevelopertips.com/objective-c/objective-c-categories.html
http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/