iOS6.0旋轉兼容的那點事
這兩天問答系統里,問ios橫豎屏切換、還有狀態欄旋轉的問題有點多,來些小心得,希望遇到的人少走彎路;
先貼官方說明:
iOS 6.0 Release Notes:
Autorotation is changing in iOS 6. In iOS 6, the shouldAutorotateToInterfaceOrientation: method
of UIViewController is deprecated.In its place, you should use the supportedInterfaceOrientationsForWindow:
and shouldAutorotate methods.
在iOS 6.0之前我們都是使用shouldAutorotateToInterfaceOrientation方法來控制視圖、狀態欄的旋轉,但是iOS 6.0及以后
就要使用supportedInterfaceOrientations方法來控制旋轉了;
所以向iOS 6.0兼容的需要手動添加supportedInterfaceOrientations 方法,來控制視圖和 狀態欄的旋轉,還有兩點需要說明:
1、在iOS 6.0之前,控制旋轉的代碼,無需和plist的中的Supported interface orientations一一對應,舉個例子:
plist的Supported interface orientations選項中支持,Portrait(bottom home button)、Landscape(right home button),無Landscape(left home button)
方法shouldAutorotateToInterfaceOrientation中強制支持UIInterfaceOrientationLandscapeLeft編譯執行沒有任何問題,
但是在iOS 6.0中,如果在supportedInterfaceOrientations中添加UIInterfaceOrientationMaskLandscapeLeft編譯正常,
運行過程中,左旋轉程序就會異常退出;所以程序支持旋轉的,代碼與plist一定要保持一致;
2、在Xcode 4.5之前旋轉支持的是
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
而Xcode 4.5 GM Seed及Xcode 4.5.1旋轉支持的是(多了個All,還有Mask的修飾,Xcode 4.5之前是不識別的)
UIInterfaceOrientationMaskAll
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskPortraitUpsideDown