UIWindow 是一種特殊的 UIView,通常在一個 App 中只會有一個。iOS 程式啟動完成後,建立的第一個視圖控制項就是 UIWindow,接著建立控制器的 view,最後把控制器的 view 加到 UIWindow 上,於是畫面就顯示出來了。可以說,一個 iOS 程式之所以能顯示在螢幕上,完全是因為它有 UIWindow;沒有 UIWindow,就看不到任何 UI 介面。
(1)[UIApplication sharedApplication].windows:本應用中開啟的 UIWindow 列表,藉此可以取得應用中的任何一個 UIView(例如輸入文字時彈出的鍵盤,就位於一個新的 UIWindow 中)。
(2)[UIApplication sharedApplication].keyWindow:取得應用程式的主視窗,用來接收鍵盤以及非觸控類的事件,而且程式在任一時刻只能有一個 keyWindow。經驗證,非 keyWindow 其實也能接收鍵盤訊息;若某個 UIWindow 內的文字方塊無法輸入,可能就是因為它不是 keyWindow。
(3)view.window:取得某個 UIView 所在的 UIWindow。
UIWindow 有三個層級:Normal、StatusBar、Alert,輸出後可發現三者數值依次為 0、1000、2000,也就是 Normal 最低、StatusBar 居中、Alert 最高。一般程式的介面位於 Normal 層級,系統頂部狀態列位於 StatusBar,而 UIActionSheet、UIAlertView 這類用來中斷流程、提醒使用者的元件則位於 Alert。依顯示層級優先原則,層級高者顯示在上層,因此程式正常顯示的 view 在最底層。
官方文件是這樣解釋的:「The key window is the one that is designated to receive keyboard and other non-touch related events. Only one window at a time may be the key window.」翻譯過來就是:keyWindow 是指定的、用來接收鍵盤以及其他非觸控事件的視窗,而且每一時刻只能有一個視窗是 keyWindow。
觀察 UIWindow 的文件,可以發現四個與視窗變化相關的通知:
UIWindowDidBecomeVisibleNotification UIWindowDidBecomeHiddenNotification UIWindowDidBecomeKeyNotification UIWindowDidResignKeyNotification
這四個通知物件中的 object 代表目前顯示(隱藏)、成為 keyWindow(不再是 keyWindow)的視窗,userInfo 則為空。我們可以註冊這四個訊息並印出資訊,觀察 keyWindow 的變化以及視窗的顯示與隱藏。
變成 keyWindow 的流程大致如下:
1. 程式預設的 window 先顯示出來。
2. 預設的 window 再變成 keyWindow。
3. AlertView 的 window 顯示出來。
4. 預設的 window 變成 keyWindow。
5. 最終 AlertView 的 window 變成 keyWindow。
iOS 7 之前,Window 的 bounds 不會隨方向變化;但到了 iOS 8 之後,隨著裝置方向旋轉,window.bounds.size.width 與 window.bounds.size.height 也會相應改變。做個簡單測試:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
return YES;
}
- (void)orientationChanged:(NSNotification *)noti {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
NSString *orientationDes = nil;
switch (orientation) {
case UIDeviceOrientationLandscapeLeft: orientationDes = @"LandscapeRight"; break;
case UIDeviceOrientationLandscapeRight: orientationDes = @"LandscapeLeft"; break;
case UIDeviceOrientationPortrait: orientationDes = @"Portrait"; break;
case UIDeviceOrientationPortraitUpsideDown: orientationDes = @"PortraitUpsideDown"; break;
default: orientationDes = @""; break;
}
NSLog(@"system ver: %@, orientaion: %@, window bounds: %@",
[UIDevice currentDevice].systemVersion,
orientationDes,
NSStringFromCGRect(self.window.bounds));
}
在 iOS 8 上執行,會看到 bounds 隨著方向改變(橫向時 width > height,豎向時 width < height):
system ver: 8.0, orientaion: LandscapeRight, window bounds: {{0, 0}, {320, 480}}
system ver: 8.0, orientaion: Portrait, window bounds: {{0, 0}, {480, 320}}
system ver: 8.0, orientaion: LandscapeLeft, window bounds: {{0, 0}, {320, 480}}
system ver: 8.0, orientaion: PortraitUpsideDown, window bounds: {{0, 0}, {480, 320}}
而在 iOS 7 及更早版本上,不論方向如何,bounds 都固定不變:
system ver: 7.0.3, orientaion: LandscapeRight, window bounds: {{0, 0}, {320, 480}}
system ver: 7.0.3, orientaion: Portrait, window bounds: {{0, 0}, {320, 480}}
system ver: 7.0.3, orientaion: LandscapeLeft, window bounds: {{0, 0}, {320, 480}}
system ver: 7.0.3, orientaion: PortraitUpsideDown, window bounds: {{0, 0}, {320, 480}}
對比之下可以清楚看到 iOS 8 在處理旋轉時的策略變更。雖然這可能因與舊版不同而導致現有專案的布局出現問題,但 iOS 8 的處理方式更貼近我們的預期,也更符合「所見即所得」的原則。
最後補充一點:不管是 iOS 7、之前的版本,還是 iOS 8,所有 ViewController 的 bounds 都是正確的。因此只要堅持一個原則——「所有布局都基於 VC.view.bounds 來做」,你的 App 顯示就不會出問題。