iOS UIWindow 與 windowLevel 學習


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  但是如果 keyWindowwindowLevel 小於其他的windowLevel 當前這個 KeyWindow 也不是在最最上層優先顯示的
  3. UIWindow有三個層級,分別是Normal,StatusBar,Alert。 這三個層級的值 從左到右依次是0,1000,2000
  4.  根據UIWindow顯示級別優先的原則,(UIWindow在顯示的時候會根據UIWindowLevel進行排序的)即Level高的將“始終”排在所有windowLevel比他低的層級的前面顯示出來。
  5. 系統默認的keyWindowwindowLevelNormal 那么 要優先顯示創建的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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM