您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

修正 iOS 6 上 Orientation 無法旋轉的問題

編輯:iOS 開發綜合

從 iOS 6 開始,蘋果調整了畫面方向與啟動視窗的相關 API,如果沿用舊寫法,可能會遇到無法旋轉或畫面異常的情形。以下整理兩個關鍵修正點。

一、把 addSubview 改成 setRootViewController

舊寫法 [window addSubview: viewController.view]; 在 iOS 6 上已經不再適用,建議依系統版本分別處理:

if ([[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
    // iOS 6 以前:addSubview 可用
    [window addSubview: viewController.view];
}
else
{
    // iOS 6:改用 setRootViewController
    [window setRootViewController:viewController];
}

二、修改旋轉判斷方法

iOS 6 以前是透過 shouldAutorotateToInterfaceOrientation 來判斷畫面是直向還是橫向:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

到了 iOS 6,則要改用 supportedInterfaceOrientations 搭配 shouldAutorotate:

// iOS 6:用 supportedInterfaceOrientations 與 shouldAutorotate 取代舊方法
- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL)shouldAutorotate {
    return YES;
}

把這兩處改好,就能讓 App 在 iOS 6 上正確支援橫向旋轉了。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved