iOS_關於手機支援的螢幕方向
設定支援的螢幕方向有兩個層級,一個是app層級的,另一個是viewController層級的。
app 層級的可以在[target]-[general]-[device orientation]裡面設定,如:
預設情況下Upside Down沒有勾選,其他都勾選了。
(為什麼Upside Down不推薦勾選呢,因為iPhone的電話app是不支援Upside Down的,如果你的app支援Upside Down,萬一使用者在用你的app的時候Upside Down了,這時候來了電話,就會看到整個來電的畫面是顛倒的,使用者體驗很不好。一向注重使用者體驗的蘋果是不推薦你勾選Upside Down的)
viewController層級的就是在各個viewController裡面設定了。
這裡有一點需要注意,viewController的設定受app層級設定的限制,也就是viewController能夠設定的螢幕方向只能是在app層級中勾選的一種或多種,沒有勾選的是不能設定的。比如上面的Upside Down沒有勾選,那麼viewController也就不能設定Upside Down的方向。
那麼在viewController裡面怎麼設定螢幕方向呢?
iOS6以前:
// 設定螢幕只支援豎向- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation == UIInterfaceOrientationPortrait);}
從iOS6開始,上面的方法已經被拋棄,有了3個新的方法:
// 不支援旋轉螢幕- (BOOL)shouldAutorotate{ return NO;}// 只支援豎向- (NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationPortrait;}// 畫面一開始載入時就是豎向// - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {// return UIInterfaceOrientationPortrait;// }
如果iOS6之前的版本也對應,那麼被拋棄的那個方法也需要加上去。
但是,iOS8.3開始,在有UIAlertView的viewController裡面,彈出UIAlertView的時候會崩潰,Log資訊如下:
Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [_UIAlertShimPresentingViewController shouldAutorotate] is returning YES'
通過查閱官方文檔,發現supportedInterfaceOrientations方法的傳回值是UIInterfaceOrientationMask類型的,所以應該用UIInterfaceOrientationMaskPortrait。UIInterfaceOrientationMask類型從iOS6就有了,只不過到iOS8.3才會崩潰。
至於preferredInterfaceOrientationForPresentation方法,傳回值還是老的UIInterfaceOrientation類型。