從 iOS 6 開始,蘋果調整了畫面方向與啟動視窗的相關 API,如果沿用舊寫法,可能會遇到無法旋轉或畫面異常的情形。以下整理兩個關鍵修正點。
舊寫法 [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 上正確支援橫向旋轉了。