整理 API 的目的很單純:當一本字典。簡單、明瞭、隨時能複習。本文把 UIWindow 的介面依「層級、事件、座標、鍵盤通知」四條線重新編排。
typedef CGFloat UIWindowLevel;
UIWindowLevel 定義了視窗的展示層級。系統在排序時,level 高的會排在 level 低的之前,也就是永遠蓋在上面。
蘋果預先定義了三個常用層級,數值本身就是優先級:
UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal; // 預設層級,值為 0,一般 App 界面都在這一層 UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar; // 值為 1000,狀態列所在層級 UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert; // 值為 2000,UIAlertView/UIAlertSheet 等中斷式提示所在層級
這也解釋了一個常見困惑:彈窗出現時,為什麼背後的界面全都無法互動?因為 alert 實際上是一個獨立且層級最高的 UIWindow,並成為當前的 keyWindow,事件自然只會交給它處理。
@property(nonatomic, retain) UIScreen *screen NS_AVAILABLE_IOS(3_2); // 預設為 [UIScreen mainScreen]。切換螢幕是昂貴操作,效能敏感路徑應避免 @property(nonatomic) UIWindowLevel windowLevel; // 預設 0.0
keyWindow 是唯一負責接收鍵盤事件與非觸碰類訊息的視窗,同一時刻全 App 只能有一個。
@property(nonatomic, readonly, getter=isKeyWindow) BOOL keyWindow; - (void)becomeKeyWindow; // 供子類覆寫的切入點,不要直接呼叫 - (void)resignKeyWindow; // 同上,放棄主視窗身分 - (void)makeKeyWindow; // 讓自己成為 keyWindow - (void)makeKeyAndVisible; // 成為 keyWindow 並同時顯示,多數 App 啟動時呼叫的就是這個
@property(nonatomic, retain) UIViewController *rootViewController NS_AVAILABLE_IOS(4_0); // 預設 nil
事件流向是這樣的:產生事件後先交給 UIApplication,UIApplication 轉給 UIWindow,UIWindow 透過 sendEvent: 尋找合適的 first responder,再分派給特定物件。sendEvent: 相當於事件的中轉站。
- (void)sendEvent:(UIEvent *)event;
- (CGPoint)convertPoint:(CGPoint)point toWindow:(UIWindow *)window; - (CGPoint)convertPoint:(CGPoint)point fromWindow:(UIWindow *)window; // 傳 nil 代表螢幕座標 - (CGRect)convertRect:(CGRect)rect toWindow:(UIWindow *)window; - (CGRect)convertRect:(CGRect)rect fromWindow:(UIWindow *)window;
UIKIT_EXTERN NSString *const UIWindowDidBecomeVisibleNotification; // 視窗顯示後 UIKIT_EXTERN NSString *const UIWindowDidBecomeHiddenNotification; // 視窗隱藏後 UIKIT_EXTERN NSString *const UIWindowDidBecomeKeyNotification; // 成為主視窗 UIKIT_EXTERN NSString *const UIWindowDidResignKeyNotification; // 撤銷主視窗
有一個細節值得注意:App 進入背景時,前兩個通知不會被觸發。即使視窗實際上不可見,系統仍認為它處於可見狀態。
UIKIT_EXTERN NSString *const UIKeyboardWillShowNotification; // 即將顯示 UIKIT_EXTERN NSString *const UIKeyboardDidShowNotification; // 已顯示 UIKIT_EXTERN NSString *const UIKeyboardWillHideNotification; // 即將隱藏 UIKIT_EXTERN NSString *const UIKeyboardDidHideNotification; // 已隱藏 UIKIT_EXTERN NSString *const UIKeyboardWillChangeFrameNotification; // frame 即將改變(iOS 5) UIKIT_EXTERN NSString *const UIKeyboardDidChangeFrameNotification; // frame 已改變(iOS 5)
通知的 userInfo 中可取出更精確的資訊:
UIKeyboardFrameBeginUserInfoKey // 動畫前鍵盤位置,NSValue 包裝的 CGRect UIKeyboardFrameEndUserInfoKey // 動畫後鍵盤位置,同上 UIKeyboardAnimationDurationUserInfoKey // 動畫持續時間,NSNumber UIKeyboardAnimationCurveUserInfoKey // 動畫曲線類型(UIViewAnimationCurve),NSNumber // 取值範例 CGRect endFrame = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
實務建議:若只需把內容推到鍵盤上方,優先監聽 WillChangeFrame 並搭配 userInfo 中的動畫參數做同步動畫,視覺效果會與系統鍵盤完全一致。若只是要調整 scrollView 的內距,直接改用 UIScrollView 的 keyboardDismissMode 與 automaticallyAdjustsScrollViewInsets 往往更省事。
以下鍵盤相關的 userInfo 鍵已長期待廢棄,新專案不必理會:
UIKeyboardCenterBeginUserInfoKey NS_DEPRECATED_IOS(2_0, 3_2); UIKeyboardCenterEndUserInfoKey NS_DEPRECATED_IOS(2_0, 3_2); UIKeyboardBoundsUserInfoKey NS_DEPRECATED_IOS(2_0, 3_2);