iOS UIWindow 與 windowLevel 學習

來源:互聯網
上載者:User

標籤:round   gre   property   touch   keyboard   添加   ted   des   分享   

Pop幾個關鍵點

  1.  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” 會使對象主視窗展示在最前端
  2.  makeKeyAndVisible  方法使 對象window成為了 KeyWindow  但是如果 keyWindow 的windowLevel 小於其他的windowLevel 當前這個 KeyWindow 也不是在最最上層優先顯示的
  3. UIWindow有三個層級,分別是Normal,StatusBar,Alert。 這三個層級的值 從左至右依次是0,1000,2000
  4.  根據UIWindow顯示層級優先的原則,(UIWindow在顯示的時候會根據UIWindowLevel進行排序的)即Level高的將“始終”排在所有windowLevel比他低的層級的前面顯示出來。
  5. 系統預設的keyWindow 的windowLevel 是 Normal 那麼 要優先顯示建立的windowLevel 必須大於等於Normal 才會展示在上層。
  6.  建立 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 學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.