在 iOS 6 裡,控制 App 是否允許旋轉的方式與舊版本有所不同,主要透過兩個方法來完成。下面整理常見的設定寫法。
如果你希望畫面永遠保持直向,只要讓 shouldAutorotate 回傳 NO 即可:
-(BOOL)shouldAutorotate
{
return NO;
}
若要開放旋轉,除了讓 shouldAutorotate 回傳 YES,還必須透過 supportedInterfaceOrientations 指定支援的方向:
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape; //可選值:UIInterfaceOrientationMaskLandscape、UIInterfaceOrientationMaskAll、UIInterfaceOrientationMaskAllButUpsideDown
}
如果你的 App 還需要向下相容舊系統,建議一併保留舊的旋轉判斷方法:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
幾個常見的方向遮罩含义如下:
UIInterfaceOrientationMaskLandscape:支援左右兩個橫向。
UIInterfaceOrientationMaskAll:支援四個方向皆可旋轉。
UIInterfaceOrientationMaskAllButUpsideDown:除了倒置之外,其餘方向都可旋轉。
掌握這幾個關鍵方法,就能針對不同畫面靈活決定要不要跟著重力感應旋轉了。