目前為了要相容 iOS 5 和 6 的旋轉,在原本的旋轉方法下,還要加入以下方法。
viewController.mm
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//僅支援直式
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(NSUInteger)supportedInterfaceOrientations
{
//僅支援直式
return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutorotate
{
return YES;
}
以為解決了,結果在 AppDelegate.m 下用
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootviewcontroller = navigationController;
這下可好,完成不理之前的設定(僅支援直式),變成任意旋轉了@_@|||
查了查才發現 self.viewController 是 UIViewController,而被加入 UINavigationController ,旋轉設定並無法影響,解決方法有兩種:
1. 改 UIViewController 為 UINavigationController 的子類別
2. 直接在 AppDelegate.m 覆寫 supportedInterfaceOrientations 方法,如下所示:
@implementation UINavigationController (iOS6OrientationFix)
-(NSUInteger) supportedInterfaceOrientations
{
//被加入的 viewController 為最上層,故只取 topViewController
return [self.topViewController supportedInterfaceOrientations];
}
@end
參考來源:
http://stackoverflow.com/questions/12410031/ios-6-rotations-supportedinterfaceorientations-doesnt-work
http://stackoverflow.com/questions/12504464/ios-6-uitabbarcontroller-supported-orientation-with-current-uinavigation-control
好了,打完收工!