設置支持的屏幕方向有兩個級別,一個是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以前:
[objc] view plain copy
// 設置屏幕只支持豎向
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
從iOS6開始,上面的方法已經被拋棄,有了3個新的方法:
[objc] view plain copy 在CODE上查看代碼片派生到我的代碼片
// 不支持屏幕旋轉
- (BOOL)shouldAutorotate
{
return NO;
}
// 只支持豎向
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationPortrait;
}
// 畫面一開始加載時就是豎向
// - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
// return UIInterfaceOrientationPortrait;
// }