转载自: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. Web开发人员不要错过的60款用户界面设计工具(下)

    41. Snipplr 包含最新的脚本和jQuery技术资源库. 42. Midori Midori是一个超轻量级的JavaScript框架,可使用CSS选择器快速访问页面上对应的元素. 43. ro ...

  2. isset() 与 empty() 的区别

    PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...

  3. SSH-KeyGen 的用法

    假设 A 为客户机器,B为目标机: 要达到的目的:A机器ssh登录B机器无需输入密码:加密方式选 rsa|dsa均可以,默认dsa 做法:1.登录A机器 2.ssh-keygen -t [rsa|ds ...

  4. Educational Codeforces Round 15_B. Powers of Two

    B. Powers of Two time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  5. mysql命令行操作 添加字段,修改字段

    alter table  t_apply   change column     remarks(原)    apply_remarks(目标)   varchar(100) default '' c ...

  6. nefu 899这也是裸的找

    #include <iostream> #include <algorithm> #include <cstdio> using namespace std; in ...

  7. MC 自己平均

    using System; using System.Drawing; using System.Linq; using System.Collections; namespace PowerLang ...

  8. C++ 中Hello World的一种写法

    /*C++ Hello World**/#include <stdio.h>#include <iostream>int main(){    printf("Hel ...

  9. 关于C#静态构造函数的几点说明

    静态构造函数是C#的一个新特性,其实好像很少用到.不过当我们想初始化一些静态变量的时候就需要用到它了.这个构造函数是属于类的,而不是属于哪里实例的,就是说这个构造函数只会被执行一次.也就是在创建第一个 ...

  10. OpenLayer 3 鹰眼控件和全屏显示

    <body> <div id="map"></div> <script> var map=new ol.Map({ target:& ...