標籤:round gre property touch keyboard 添加 ted des 分享
Pop幾個關鍵點
- KeyWindow :”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” 會使對象主視窗展示在最前端
- makeKeyAndVisible 方法使 對象window成為了 KeyWindow 但是如果 keyWindow 的windowLevel 小於其他的windowLevel 當前這個 KeyWindow 也不是在最最上層優先顯示的
- UIWindow有三個層級,分別是Normal,StatusBar,Alert。 這三個層級的值 從左至右依次是0,1000,2000
- 根據UIWindow顯示層級優先的原則,(UIWindow在顯示的時候會根據UIWindowLevel進行排序的)即Level高的將“始終”排在所有windowLevel比他低的層級的前面顯示出來。
- 系統預設的keyWindow 的windowLevel 是 Normal 那麼 要優先顯示建立的windowLevel 必須大於等於Normal 才會展示在上層。
- 建立 UIWindow 不用添加到任何的控制項上面,直接建立完畢 即自動添加到UIWindow 上 建立方式 展示 銷毀 都和 一般 UIView 的方式有區別 參見代碼注釋
:
參見代碼:
//// HFWindowViewController.m// SectionDemo//// Created by HF on 2017/5/25.// Copyright ? 2017年 HF-Liqun. All rights reserved.//#import "HFWindowViewController.h"@interface HFWindowViewController ()// 建立屬性@property (nonatomic, strong)UIWindow *myWindow1;@end@implementation HFWindowViewController- (void)viewDidLoad { [super viewDidLoad]; // 建立測試按鈕 UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeSystem]; tempBtn.frame = CGRectMake(15,64, self.view.frame.size.width - 15 * 2, 64); [tempBtn setTitle:@"點我建立一個window" forState:UIControlStateNormal]; // 通過按鈕的點擊事件產生不同windowLevel層級的window [tempBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:tempBtn];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark- event- (void)clickBtn:(id)sender{ [self test1];}- (void)clickWindowBtn:(id)sender{ //window 銷毀 self.myWindow1.hidden = YES; //可有可無 看 UI效果 self.myWindow1 = nil; // 這個方法是真正移除 UIWindow}#pragma mark - private/** * *1、建立 window 不用添加到任何的控制項上面,直接建立完畢 即自動添加到window 上 *2、建立一個比預設window的windowLevel大的window來看一下什麼效果,效果是會蓋在原來的window上面 */- (void)test1{ // 建立window if (self.myWindow1 == nil) { if (IOS9) {//>=iOS9 self.myWindow1 = [UIWindow new]; // 以後 預設了 window的大小 } else { self.myWindow1 = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];//這麼寫在哪個版本系統上,一點毛病都沒有 } UIButton *windowBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [windowBtn setTitle:@"點我 銷毀黃色 window" forState:UIControlStateNormal]; windowBtn.backgroundColor = [UIColor redColor]; windowBtn.frame = CGRectMake(15, 64, self.view.frame.size.width - 15 * 2, 64); [windowBtn addTarget:self action:@selector(clickWindowBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.myWindow1 addSubview:windowBtn]; } // 設定window的顏色,這裡設定成黃色,方便查看window的層級關係 self.myWindow1.backgroundColor = [UIColor yellowColor]; // 設定 window 的 windowLevel self.myWindow1.windowLevel = UIWindowLevelStatusBar; //TODO: Normal,StatusBar,Alert 分別 為 0,1000,2000 可以修改這裡體驗 層級變化 對 展示 window的影響 self.myWindow1.hidden = NO; [self.myWindow1 makeKeyAndVisible]; //成為keyWindow}@end
iOS UIWindow 與 windowLevel 學習