1. 程式人生 > >iPhone開發筆記(21)iOS 6旋轉問題解決方法彙總

iPhone開發筆記(21)iOS 6旋轉問題解決方法彙總

    iOS 6的SDK改變了以往控制UIViewController的方式,為了相容iOS 5和iOS 6,需要對程式碼進行必要的調整。因為每個應用的結構不一樣,所以再這篇文章中,我只講了我所遇到的UITabBarController+UINavigationController的應用結構。此外,我也在最後列出了一些情況的解決方法,如果本文的方法對你遇到的問題不起作用,那麼可以試試列出的連線給出的解決方法。

    1、在工程的設定介面將裝置支援的旋轉方向開關開啟如下圖:

    

    2、設定window的rootViewController

    在AppDelegate.h中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ......
    self.window.rootViewController = tabBarController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    return YES;
}

    3、新增UITabBarController的Category

@implementation UITabBarController (Background)

-(BOOL)shouldAutorotate
{
    //這裡我是首先從全域性的角度設定自動旋轉為NO,因為不需要每個UIViewController都自動旋轉。
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    //全域性設定為
    return UIInterfaceOrientationMaskPortrait;
}

@end

@implementation AppDelegate
    4、在需要開啟自動旋轉的UIViewController的.m檔案中新增如下程式碼
//iOS 5以前的旋轉控制方法
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return ((interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown) &&
                (interfaceOrientation != UIInterfaceOrientationLandscapeLeft) &&
                (interfaceOrientation != UIInterfaceOrientationLandscapeRight));
    } else {
        return YES;
    }
}

/iOS 6的旋轉控制方法
- (BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
    5、其他情況的解決方法參考

http://stackoverflow.com/questions/12522903/uitabbarcontroller-rotation-issues-in-ios-6