分析UIWindow
转载自: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的更多相关文章
- 【转】iOS应用崩溃日志分析
作为一名应用开发者,你是否有过如下经历? 为确保你的应用正确无误,在将其提交到应用商店之前,你必定进行了大量的测试工作.它在你的设备上也运行得很好,但是,上了应用商店后,还是有用户抱怨会闪退 ! ...
- iOS 第三方自定义Alertview项目MBProcessHud中的重要代码分析
做ios,弹出一个自定义的alertview挺常见的.ios7以前,我们可以对系统的UIAlertView进行一点操作,实现一点简单的定制,但是ios7不再允许我们这样做了.因此,我们需要自己创建一个 ...
- UIWindow 详解
UIWindow对象是所有UIView的根视图,管理和协调的应用程序的显示.分发事件给View.UIWindow类是UIView的子类,可以看作是特殊的UIView.一般应用程序只有一个UIWindo ...
- cocos2D(三)---- 第一cocos2d的程序代码分析
在第一讲中已经新建了第一个cocos2d程序,执行效果例如以下: 在这讲中我们来分析下里面的代码,了解cocos2d的工作原理,看看屏幕上的这个"Hello World"是怎样显示 ...
- IOS计划 分析
1.基本介绍 IOS苹果公司iPhone.iPod touch和iPad操作系统和其他设备的发展. 2.知识点 1.IOS系统 iPhone OS(现在所谓的iOS)这是iPhone, iPod to ...
- [转]MBProgressHUD 源码分析
源码来源: https://github.com/jdg/MBProgressHUD 版本:0.9.1 MBProgressHUD是一个显示HUD窗口的第三方类库,用于在执行一些后台任务时,在程序中显 ...
- JDiPad项目runtime的使用分析
首先,项目有点老 但是运行还是没有问题的.其中很多地方到了runtime,同时也看到了 早期的开发人员 基本没用pod 第三方也很少用,除了微信登录,整个项目还没看到集成的第三方SDK.然后慢慢梳理 ...
- 三、第一个cocos2d程序的代码分析
http://blog.csdn.net/q199109106q/article/details/8591706 在第一讲中已经新建了第一个cocos2d程序,运行效果如下: 在这讲中我们来分析下里面 ...
- 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 ...
随机推荐
- 一个基于DpperHelper的t4模板
自定义模板,空的类(目的是和t4生成的模板分开,以免被覆盖) ExtensionDAL <#@ template debug="false" hostspecific=&qu ...
- ubuntu/linux 下 git 通过代理下载数据 (最简单的方式)
git国内访问较慢,走代理较快. 方法:git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:port_n ...
- Infix expressions 中缀表达式
中缀表达式的计算 利用两个栈来实现,操作数栈,操作符栈 只支持个位数运算 最后必须输入一个'#' #include<iostream> using namespace std; templ ...
- 利用proxychains在终端使用socks5代理
最近用各种脚本下载东西的时候发现有的站点需要当地IP才能下,比如.....nico, youtube等: 所以就找了下能在终端用socks5代理的工具,最后找到了proxychains,从此再无压力= ...
- springmvc权限拦截器
配置文件spring-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- UICollectionView之自定义Layout
#import <UIKit/UIKit.h> @interface WQViewController : UIViewController - (id)initWithFrame:(CG ...
- $.data()、$().data
两个方法很相似,但是有区别,简单说一下: $.data():jq的静态方法,也就是jQuery.data()直接调用 $().data():实例方法,先有实例,才能调用这个方法,例如:$(" ...
- shell 变量说明
变量说明 $$Shell本身的PID(ProcessID)$!Shell最后运行的后台Process的PID$?最后运行的命令的结束代码(返回值)$-使用Set命令设定的Flag一览$*所有参数列表. ...
- git 使用总结
- linq any() all() 返回true 或者false
一.any()只要有一个符合条件就返回true static void Main(string[] args) { //any 有符合条件的就返回true ,,,,,,,,,}; ); Console ...