您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 6 屏幕方向控制:鎖定與旋轉設定

編輯:iOS 開發綜合

在 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:除了倒置之外,其餘方向都可旋轉。

掌握這幾個關鍵方法,就能針對不同畫面靈活決定要不要跟著重力感應旋轉了。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved