天天看點

【Xamarin挖牆腳系列:IOS-關于手機支援的螢幕方向】

設定支援的螢幕方向有兩個級别,一個是app級别的,另一個是viewController級别的。

【Xamarin挖牆腳系列:IOS-關于手機支援的螢幕方向】

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類型。

繼續閱讀