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

iOS 螢幕方向控制完整整理:自動、手動與強制旋轉

編輯:iOS 開發綜合

控制螢幕方向是每個 App 都會碰到的題目,也是 iOS 6 之後改動幅度相當大的一塊。本文把自動旋轉、手動旋轉、強制旋轉三種做法與它們的陷阱一次講清楚。

一、從 shouldAutorotateToInterfaceOrientation 說起

iOS 6 之前,控制方向靠的是 shouldAutorotateToInterfaceOrientation:。要讓哪個 view controller 支援旋轉,就在裡面覆寫這個方法。

iOS 6 之後它被正式標記為廢棄:

// Applications should use supportedInterfaceOrientations and/or shouldAutorotate.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    NS_DEPRECATED_IOS(2_0, 6_0);

取而代之的是這兩個方法:

- (BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;

此外還有一個硬性前提:必須在 Info.plist 中列出 App 支援的所有方向,否則無論程式碼怎麼寫都不會旋轉。設定方式有兩種:

1.

2.

若還要相容 iOS 6 以前的系統,則需保留 shouldAutorotateToInterfaceOrientation: 以及 willRotate 系列方法。

二、自動旋轉:方法要寫在 rootViewController

這是最容易搞錯的地方。控制某個 view controller 的方向,不是在它自己身上覆寫那兩個方法,而是要寫在它的根視圖控制器——也就是直接掛在 self.window 上的那一層:

UIViewController *viewCtrl = [[UIViewController alloc] init];
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];

if ([window respondsToSelector:@selector(setRootViewController:)]) {
    self.window.rootViewController = navCtrl;
} else {
    [self.window addSubview:navCtrl.view];
}

若要控制 viewCtrl 的旋轉,就不能寫在 UIViewController 裡,而要在 navCtrl 上設定。由於 UINavigationController 是系統類別,需要繼承一個子類別再覆寫:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (BOOL)shouldAutorotate {
    return YES;
}

情況一:如果 rootViewController 直接就是 viewCtrl(沒有 navCtrl 包裝),那這兩個方法就該寫在 viewCtrl 自己身上。

情況二:viewCtrl 又 push 了 viewCtrl2,想單獨控制 viewCtrl2?還是在 navCtrl 裡處理,因為兩者的 rootViewController 都是 navCtrl:

UIViewController *viewCtrl2 = [[UIViewController alloc] init];
[self.navigationController.navigationController pushViewController:viewCtrl2 animated:YES];

此時 navCtrl 內部必須自行判斷「目前是哪一個 view controller」才能分別給定規則。同理,若 rootViewController 是 UITabBarController,就繼承 UITabBarController 並在其中覆寫、逐一控制。

三、通用解法:讓每個頁面自己決定

問題來了:建立 UINavigationController 的時候,往往不確定未來會 push 哪些頁面。這時最乾淨的做法是把決定權交還給各頁自己,navCtrl 只負責轉述:

- (BOOL)shouldAutorotate {
    return self.topViewController.shouldAutorotate;
}

- (NSUInteger)supportedInterfaceOrientations {
    return self.topViewController.supportedInterfaceOrientations;
}

這樣一來,只要在 viewCtrl、viewCtrl2 各自覆寫這兩個方法,就能獨立設定自己的方向,navCtrl 完全不必知道它們的存在。這是擴充性最好的一種寫法。

情況三:如果 viewCtrl 是透過 presentModalViewController 跳出 viewCtrl3,那 viewCtrl3 的方向就與 navCtrl 無關了。若 viewCtrl3 被新的 navController 或 tabBarController 包裝過,就在新包裝的容器裡設定;若是直接 present,就在 viewCtrl3 自己身上設定。

四、手動旋轉

有兩條路,其中一條不建議使用。

方式一:直接改 UIDevice 的 orientation(不推薦)

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
    [[UIDevice currentDevice] performSelector:@selector(setOrientation:)
                                   withObject:(id)UIInterfaceOrientationPortrait];
}

這是私有 API,上傳 App Store 有被拒的風險,請勿用於正式產品。

方式二:假旋轉(推薦)

不動 UIDevice,只改變某個 view 的 transform:

self.view.transform = CGAffineTransformMakeRotation(M_PI / 2);

完整思路是:先設定 statusBarOrientation,再把 view 轉到與之一致的方向。

既然是手動旋轉,首先得關掉自動旋轉:

- (BOOL)shouldAutorotate {
    return NO;
}

然後在按鈕事件裡觸發:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
self.view.transform = CGAffineTransformMakeRotation(M_PI / 2);
self.view.bounds = CGRectMake(0, 0, kScreenHeight, 320);

兩個重點:

  • 只要改 self.view.transform,它所有的 subview 都會跟著轉。方向變了,尺寸也要重設——這裡必須用 bounds 而不是 frame,因為 transform 非 identity 時 frame 的語意已經失真。
  • shouldAutorotate 必須回傳 NO,setStatusBarOrientation 才會生效。這是很多人卡住的地方。

五、強制旋轉設備

最近接手一個專案,用的是 MWPhotoBrowser 這個第三方圖片瀏覽庫。正常情況沒問題,但當裝置處於橫屏狀態時 modal 這個頁面,圖片位置就會錯亂。

實在沒有更好的解法,只好在 modal 之前用程式碼把裝置強制轉回直向。

// UIDevice+WJ.h
@interface UIDevice (WJ)

/**
 *  強制旋轉裝置
 *
 *  @param orientation 目標方向
 */
+ (void)setOrientation:(UIInterfaceOrientation)orientation;

@end
// UIDevice+WJ.m
#import "UIDevice+WJ.h"

@implementation UIDevice (WJ)

// 透過 NSInvocation 呼叫私有方法
+ (void)setOrientation:(UIInterfaceOrientation)orientation {
    SEL selector = NSSelectorFromString(@"setOrientation:");
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[self currentDevice]];

    int val = orientation;
    [invocation setArgument:&val atIndex:2];

    [invocation invoke];
}

@end

坦白說這是個應急手段,同樣依賴私有 API,有審核風險。更穩健的方向是修正第三方庫在橫屏下的版面計算,或改用支援方向的替代方案。

總結

  • Info.plist 是前提。沒在裡面宣告的方向,程式碼寫再多也無效。
  • 自動旋轉看 rootViewController。容器類別轉述 topViewController 是最通用的寫法。
  • 手動旋轉用 transform + bounds,並把 shouldAutorotate 關掉。
  • 私有 API 一律避開。強制旋轉可以當臨時方案,但別留在要送審的程式碼裡。
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved