iOS6手持方向處理
從iOS 5的應用程式更新到iOS6很多特性沒法正常工作。主要的問題是,有一些API在新的SDK中已被棄用。其中手持方向的判斷就是很明顯的一個
存在的問題
假如你應用程式只有一個屏要是橫向,其它的屏都要是縱向。
iOS 5的解決方案
在應用程式的Info.plist檔案,Supported interface orientations應該只包含一個項目,Portrait 。
接下來,在需要的方向鎖定為橫向視圖控制器類,你需要重寫- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation方法,並返回YES或NO相對一個布爾值,檢查對interfaceOrientation參數。
下面是函數看起來像什麼。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
iOS的6解決方案
在iOS 6 -(BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation 方法已淘汰,似乎沒有被調用了。代替它的一組方法;-(BOOL)shouldAutorotate 和-(NSUInteger)supportedInterfaceOrientations的。
在UIViewController,你要在橫向的,你需要同時重寫- (BOOL)shouldAutorotate- (NSUInteger)supportedInterfaceOrientations的:
// iOS6中過時, 為了相容iOS5.
// ---
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
// iOS6 support
// ---
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
// ---