iOS_關於手機支援的螢幕方向

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.