博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分析UIWindow
阅读量:6708 次
发布时间:2019-06-25

本文共 6232 字,大约阅读时间需要 20 分钟。

转载自:http://www.cnblogs.com/YouXianMing/p/3811741.html

 

The UIWindow class defines an object known as a window that manages and coordinates the views an app displays on a device screen. Unless an app can display content on an external device screen, an app has only one window.

UIWindow定义了一个对象,用来管理视图的坐标系统,除非,app可以在另外一个窗口里面展示内容,否则,一个app只有一个window.

 

每一个controller都会被UIWindow接管,UIWindow一次只能接管一个controller,下面用代码验证.

首先,我们来看看,UIWindow何时接管了controller.

请写以下代码:

////  RootViewController.m//  Window////  Copyright (c) 2014年 Y.X. All rights reserved.//#import "RootViewController.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad{    [super viewDidLoad];        if (self.view.window)    {        NSLog(@"viewDidLoad");        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));    }}- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];        if (self.view.window)    {        NSLog(@"viewWillAppear");        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));    }}- (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];        if (self.view.window)    {        NSLog(@"viewDidAppear");        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));    }}- (void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];        if (self.view.window)    {        NSLog(@"viewWillDisappear");        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));    }}- (void)viewDidDisappear:(BOOL)animated{    [super viewDidDisappear:animated];        if (self.view.window)    {        NSLog(@"viewDidDisappear");        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));    }}@end

打印信息,注意哦,UIWindow会在一个controller的viewDidAppear方法中才接管了当前controller,而不是在ViewDidLoad方法中,注意:)

 

接下来,我们在导航栏控制器间切换,来观察,UIWindow如何放弃一个controller然后接管另外一个controller

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    UINavigationController *NC = \        [[UINavigationController alloc] initWithRootViewController:[RootViewController new]];        self.window.rootViewController = NC;        self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}
////  RootViewController.m//  Window////  Copyright (c) 2014年 Y.X. All rights reserved.//#import "RootViewController.h"#import "SecondViewController.h"#import "YXGCD.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad{    [super viewDidLoad];        if (self.view.window)    {        NSLog(@"viewDidLoad");        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));    }        [[GCDQueue mainQueue] execute:^{        [self.navigationController pushViewController:[SecondViewController new]                                             animated:YES];    } afterDelay:NSEC_PER_SEC * 4];}- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];        if (self.view.window)    {        NSLog(@"viewWillAppear");        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));    }}- (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];        if (self.view.window)    {        NSLog(@"viewDidAppear");        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));    }}- (void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];        if (self.view.window)    {        NSLog(@"viewWillDisappear");        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));    }}- (void)viewDidDisappear:(BOOL)animated{    [super viewDidDisappear:animated];        if (self.view.window)    {        NSLog(@"viewDidDisappear");        NSLog(@"%@", NSStringFromCGRect(self.view.window.frame));    }}@end
////  SecondViewController.m//  Window  Copyright (c) 2014年 Y.X. All rights reserved.//#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad{    [super viewDidLoad];    self.view.backgroundColor = [UIColor redColor];}@end

查看结果:

结论很自然,一个UIWindow只能在接管一个controller.

 

为什么要纠结于UIWindow的这些小细节呢?因为,UIWindow有着比一切controller都要高的优先级显示权利,加载在UIWindow上面的View是不会被遮挡住的.

效果(注意查看上面的导航条的地方,也被遮盖住了):

源码:

////  RootViewController.m//  Window////  Copyright (c) 2014年 Y.X. All rights reserved.//#import "RootViewController.h"#import "YXGCD.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad{    [super viewDidLoad];        [[GCDQueue mainQueue] execute:^{                UIView *redView         = [[UIView alloc] initWithFrame:self.view.window.frame];        redView.backgroundColor = [UIColor redColor];        redView.alpha           = 0.f;        [self.view.window addSubview:redView];                [UIView animateWithDuration:1 animations:^{            redView.alpha = 1.f;        } completion:^(BOOL finished) {                        [UIView animateWithDuration:1 animations:^{                redView.alpha = 0.f;            } completion:^(BOOL finished) {                [redView removeFromSuperview];            }];        }];            } afterDelay:NSEC_PER_SEC * 10];}@end

普通的View的加载的效果(注意上方的导航栏,它并没有变成红色):

////  RootViewController.m//  Window////  Copyright (c) 2014年 Y.X. All rights reserved.//#import "RootViewController.h"#import "YXGCD.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad{    [super viewDidLoad];        [[GCDQueue mainQueue] execute:^{                UIView *redView         = [[UIView alloc] initWithFrame:self.view.window.frame];        redView.backgroundColor = [UIColor redColor];        redView.alpha           = 0.f;        [self.view addSubview:redView];                [UIView animateWithDuration:1 animations:^{            redView.alpha = 1.f;        } completion:^(BOOL finished) {                        [UIView animateWithDuration:1 animations:^{                redView.alpha = 0.f;            } completion:^(BOOL finished) {                [redView removeFromSuperview];            }];        }];            } afterDelay:NSEC_PER_SEC * 10];}@end

其实呢,你如果写成这样子,也是可以遮盖导航条的呢:

其实,就这么多东西:)

 

你可能感兴趣的文章
PDMS RVM TO 3DXML - RvmTranslator6.0
查看>>
《数学与泛型编程:高效编程的奥秘》一3.2 筛选素数
查看>>
不想被攻击,5款便携式反病毒和反恶意软件帮到你
查看>>
【投资人不懂AI】为什么说AI创业不是4、5个人的团队就能搞定的事
查看>>
ARM公司收购Apical,欲致力推进“目联网”技术
查看>>
《Java核心技术 卷Ⅱ 高级特性(原书第10版)》一3.3.2 XML Schema
查看>>
《机器人自动化:建模、仿真与控制》——1.5 习题解答
查看>>
积水成渊——数据中心用水效率分析
查看>>
重新定义云数据库 阿里云POLARDB 9月21日发布
查看>>
物联网安全威胁剧增 如何拓展移动化能力
查看>>
工业物联网:创造价值 转换业务模式
查看>>
思科若要加入超融合大战:需启用你的现金
查看>>
程序员如何既不耽误工作又有时间干业余项目?
查看>>
王胤:我是怎么把体温计变成助孕计的
查看>>
Linux下如何定制SSH来简化远程访问
查看>>
空与非空 EMPTY_LOB和NULL的区别
查看>>
未来的主角是公有云还是私有云?哪些云安全企业能在行业洗牌中脱颖而出
查看>>
可能吞噬硬件的无服务器云
查看>>
如何自行搭建一个威胁感知大脑 SIEM?| 硬创公开课
查看>>
安全圈老司机为什么会在这个游戏里翻车?(内附详细解谜攻略)
查看>>