转载自: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

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

其实,就这么多东西:)

分析UIWindow的更多相关文章

  1. 【转】iOS应用崩溃日志分析

    作为一名应用开发者,你是否有过如下经历?   为确保你的应用正确无误,在将其提交到应用商店之前,你必定进行了大量的测试工作.它在你的设备上也运行得很好,但是,上了应用商店后,还是有用户抱怨会闪退 ! ...

  2. iOS 第三方自定义Alertview项目MBProcessHud中的重要代码分析

    做ios,弹出一个自定义的alertview挺常见的.ios7以前,我们可以对系统的UIAlertView进行一点操作,实现一点简单的定制,但是ios7不再允许我们这样做了.因此,我们需要自己创建一个 ...

  3. UIWindow 详解

    UIWindow对象是所有UIView的根视图,管理和协调的应用程序的显示.分发事件给View.UIWindow类是UIView的子类,可以看作是特殊的UIView.一般应用程序只有一个UIWindo ...

  4. cocos2D(三)---- 第一cocos2d的程序代码分析

    在第一讲中已经新建了第一个cocos2d程序,执行效果例如以下: 在这讲中我们来分析下里面的代码,了解cocos2d的工作原理,看看屏幕上的这个"Hello World"是怎样显示 ...

  5. IOS计划 分析

    1.基本介绍 IOS苹果公司iPhone.iPod touch和iPad操作系统和其他设备的发展. 2.知识点 1.IOS系统 iPhone OS(现在所谓的iOS)这是iPhone, iPod to ...

  6. [转]MBProgressHUD 源码分析

    源码来源: https://github.com/jdg/MBProgressHUD 版本:0.9.1 MBProgressHUD是一个显示HUD窗口的第三方类库,用于在执行一些后台任务时,在程序中显 ...

  7. JDiPad项目runtime的使用分析

    首先,项目有点老 但是运行还是没有问题的.其中很多地方到了runtime,同时也看到了 早期的开发人员 基本没用pod 第三方也很少用,除了微信登录,整个项目还没看到集成的第三方SDK.然后慢慢梳理 ...

  8. 三、第一个cocos2d程序的代码分析

    http://blog.csdn.net/q199109106q/article/details/8591706 在第一讲中已经新建了第一个cocos2d程序,运行效果如下: 在这讲中我们来分析下里面 ...

  9. iOS应用崩溃日志分析 iOS应用崩溃日志揭秘

    转自:http://www.raywenderlich.com/zh-hans/30818/ios%E5%BA%94%E7%94%A8%E5%B4%A9%E6%BA%83%E6%97%A5%E5%BF ...

随机推荐

  1. bzoj1977

    1977: [BeiJing2010组队]次小生成树 Tree Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 3001  Solved: 751[Su ...

  2. Windows线程同步(上)

    先介绍一个创建线程的API,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms682453%28v=vs.85%29.aspx ...

  3. 百度地图与融云的“冲突”(APP的.so手机架构目录,与Library的.so的手机架构目录冲突)

    在项目引进融云的IMkit时,总是报百度地图的错误,最开始以为是65535的错误,然后试着去改下百度地图,错误原因是在IMKit里面,它的.so库有这么几个目录 而我的app的libs里面的百度地图的 ...

  4. 使用bind实现主从DNS服务器数据同步

    一.bind简介 Linux中通常使用bind来实现DNS服务器的架设,bind软件由isc(www.isc.org)维护.在yum仓库中可以找到软件,配置好yum源,直接使用命令yum instal ...

  5. QML之使用Loader加载QML组件

    呵呵,今晚是边看<裸婚时代>边敲代码,那电影看得...!钱真他妈不是个东西. 盼望Meego火起来. QML的Loader元素经常备用来动态加载QML组件.可以使用source属性或者so ...

  6. Puppent 介绍原理及安装

    Puppet原理: Puppet是一个或者多个master,众多client,所有的客户端都定期(默认为30分钟)使用facter工具把 客户端的基本信息,通过https的xmlrpc协议发送给服务器 ...

  7. 准备着手学习python

    1. python 的框架 Tornado 网址: http://www.tornadoweb.org/en/stable/ github: https://github.com/tornadoweb ...

  8. Spring Boot 系列教程15-页面国际化

    internationalization(i18n) 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式. 它要求从产品中抽离所有地域语言,国家/地区和 ...

  9. Learning Java characteristics (Java in a Nutshell 6th)

    Java characteristics: Java .class files are machine-independent, including the endianness. Java .cla ...

  10. 2014 Shanghai Invitation Contest

    题目链接 http://acm.hdu.edu.cn/search.php?field=problem&key=2014%C9%CF%BA%A3%C8%AB%B9%FA%D1%FB%C7%EB ...