iOS 6.0 引入了新的旋转方法并且不调用shouldAutorotateToInterfaceOrientation:
.
您需要实现这些方法以同时支持 pre-iOS 6.0 和 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;
}
编辑-如果您UIViewController
在 aUINavigationController
的层次结构中
如果您的视图控制器在 aUINavigationController
的视图层次结构中,则实际上在导航控制器(而不是视图控制器)上调用了旋转方法……在我看来,Apple 实现了这一点很有趣,但是您可以采取以下措施来允许视图控制器来响应这些方法,而不是-
使用以下方法创建一个类别UINavigationController
:
// 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
编辑 2 - 如果您UIViewController
在 aUITabBarController
的标签内
如果您的视图控制器位于 aUITabBarController
的选项卡中,也会出现同样的问题。
再次创建一个类别UITabBarController
以允许视图控制器响应:
// 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
编辑 3
这里还有一些关于 Objective-C 类别的链接:
http://macdevelopertips.com/objective-c/objective-c-categories.html
http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/