您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

深入 UIWindow:三個層級、子層級與 keyWindow 的真相

編輯:iOS 相關教學

每一個 iOS App 背後都有一個 UIWindow。用 Xcode 的單頁模板建立專案時,系統會自動幫你產生一個 window,設為 keyWindow 並顯示出來。一切來得如此自然,以至於我們常常忘了:window 其實是可以自己建立的。

最典型的自訂場景,是在 iOS 5 以前想替換 UIAlertView 的背景樣式——由於系統樣式無法客製,工程師乾脆自己開一個 windowLevel 設為 Alert 層級的透明視窗來承載自訂畫面。

一、UIWindowLevel:三個層級與隱藏的子層級

UIWindow 定義了三個預設層級,數值剛好各差 1000:

2012-03-27 22:46:08.752 Normal window level: 0.000000
2012-03-27 22:46:08.754 Alert  window level: 2000.000000
2012-03-27 22:46:08.755 Status window level: 1000.000000

大小關係為 Normal < StatusBar < Alert。一般 App 的介面都在 Normal 層,系統頂部狀態列在 StatusBar 層,而 UIAlertView、UIActionSheet 這類需要中斷操作流程、強制使用者注意的元素,則落在最高的 Alert 層。

仔看 UIWindow 的頭檔會發現一個未公開的實例變數 CGFloat _windowSublevel;。既然三個層級之間空了 1000 的間距,那中間顯然還能塞入無數個自訂層級。實際打印驗證的結果很有意思:系統 UIAlertView 的實際層級是 1996,UIActionSheet 則是 2001——正好卡在 Alert 的 2000 上下,證實了 sublevel 的存在。

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert View"
                                                    message:@"Hello World, i'm AlertView!!!"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:@"Cancel", nil];
[alertView show];
[alertView release];

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"ActionSheet"
                                                       delegate:nil
                                              cancelButtonTitle:@"Cancel"
                                         destructiveButtonTitle:@"Don't do that!"
                                              otherButtonTitles:@"Hello World", nil];
[actionSheet showInView:self.view];
[actionSheet release];

顯示規則很單純:層級高者在上,層級低者在下,一般程式的內容永遠墊在最底層。

下面這段測試程式同時建立了四個不同層級的 window:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.backgroundColor = [UIColor yellowColor];
    [self.window makeKeyAndVisible];

    UIWindow *normalWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    normalWindow.backgroundColor = [UIColor blueColor];
    normalWindow.windowLevel = UIWindowLevelNormal;
    [normalWindow makeKeyAndVisible];

    CGRect windowRect = CGRectMake(50, 50,
                                   [[UIScreen mainScreen] bounds].size.width - 100,
                                   [[UIScreen mainScreen] bounds].size.height - 100);
    UIWindow *alertLevelWindow = [[UIWindow alloc] initWithFrame:windowRect];
    alertLevelWindow.windowLevel = UIWindowLevelAlert;
    alertLevelWindow.backgroundColor = [UIColor redColor];
    [alertLevelWindow makeKeyAndVisible];

    UIWindow *statusLevelWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 50, 320, 20)];
    statusLevelWindow.windowLevel = UIWindowLevelStatusBar;
    statusLevelWindow.backgroundColor = [UIColor blackColor];
    [statusLevelWindow makeKeyAndVisible];

    NSLog(@"Normal window level: %f", UIWindowLevelNormal);
    NSLog(@"Alert window level: %f", UIWindowLevelAlert);
    NSLog(@"Status window level: %f", UIWindowLevelStatusBar);
    return YES;
}

執行結果印證了兩件事:

  1. normalWindow 雖然在預設 window 之後才呼叫 makeKeyAndVisible,卻完全沒顯示。可見同層級時只有第一個被設為 keyWindow 的會顯示,後續同層再設也沒用。
  2. statusLevelWindow 比 alertLevelWindow 晚呼叫,卻依然被壓在下方。可見 UIWindow 排序時完全不看誰是 keyWindow,只認 windowLevel——層級最高者永遠在最前面。

二、keyWindow 到底是什麼

官方文件的說法是:key window 是被指定用來接收鍵盤與其他非觸碰類事件的視窗,且同一時刻只能有一個 window 擔任 keyWindow。

那麼非 keyWindow 是不是真的收不到鍵盤事件?我們來做個實驗:在預設 window 的 view 上放一個 UITextField,再開一個 Alert 層級的 window 設為 keyWindow 並顯示。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.viewController = [[[SvUIWindowViewController alloc] initWithNibName:@"SvUIWindowViewController"
                                                                     bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    UIWindow *window1 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 80, 320, 320)];
    window1.backgroundColor = [UIColor redColor];
    window1.windowLevel = UIWindowLevelAlert;
    [window1 makeKeyAndVisible];

    return YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self registerObserver];

    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
    field.placeholder = @"Input something here";
    field.clearsOnBeginEditing = YES;
    field.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:field];
    [field release];
}

結果頗為反直覺:即使我們另開了一個 window 並設為 keyWindow,點擊預設 window 上的 textField 依然能喚出鍵盤並正常輸入,只不過鍵盤被上層的紅色視窗遮住了。也就是說,非 keyWindow 一樣能接收鍵盤消息,這點與文件描述並不完全一致。

用通知觀察 keyWindow 的交接過程

UIWindow 提供了四個通知,可以拿來追蹤視窗的顯示與焦點變化:

UIWindowDidBecomeVisibleNotification
UIWindowDidBecomeHiddenNotification
UIWindowDidBecomeKeyNotification
UIWindowDidResignKeyNotification

這些通知的 object 就是當事 window,userInfo 則為空。註冊之後打印,可以看到完整流程:

  1. 程式預設的 window 先顯示出來
  2. 預設 window 接著成為 keyWindow
  3. AlertView 的 window 顯示出來
  4. 預設 window 退為非 keyWindow
  5. AlertView 的 window 最終成為 keyWindow

一句話總結:「想當老大,得先從小弟混起」——每個 window 都必須先經歷 resignKey 與 becomeKey 的交接。同時也印證了先前打印的層級數字:預設 window 是 0(Normal),AlertView 的 window 是 1996,比 Alert 的 2000 略低一點。

若改為在 viewDidAppear 中彈出 UIActionSheet,流程完全相同,只是 ActionSheet 的 window 層級變成 2001。

最後,當使用者點擊 ActionSheet 的取消鈕,控制台會依序出現:

  1. ActionSheet 的 window 退為非 keyWindow
  2. 程式預設的 window 重新成為 keyWindow
  3. ActionSheet 的 window 隱藏

同樣一句話:「想隱居幕後可以,但得先交出權力。」

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