(转摘+修改) 深入理解 UIWindow & UIWindowLevel

转载于: http://www.cnblogs.com/smileEvday/archive/2012/03/27/2420362.html



一、UIWindow是一种特殊的UIView,通常在一个程序中只会有一个UIWindow,但可以手动创建多个UIWindow,同时加到程序里面。UIWindow在程序中主要起到三个作用:

  1、作为容器,包含app所要显示的所有视图

  2、传递触摸消息到程序中view和其他对象

  3、与UIViewController协同工作,方便完成设备方向旋转的支持

二、通常我们可以采取两种方法将view添加到UIWindow中:

  1、addSubview

  直接将view通过addSubview方式添加到window中,程序负责维护view的生命周期以及刷新,但是并不会为去理会view对应的ViewController,因此采用这种方法将view添加到window以后,我们还要保持view对应的ViewController的有效性,不能过早释放。(需要自己维护UIViewController的生命周期)

  2、rootViewController

  rootViewController时UIWindow的一个遍历方法,通过设置该属性为要添加view对应的ViewController,UIWindow将会自动将其view添加到当前window中,同时负责ViewController和view的生命周期的维护,防止其过早释放

三、WindowLevel

  UIWindow在显示的时候会根据UIWindowLevel进行排序的,即Level高的将排在所有Level比他低的层级的前面。下面我们来看UIWindowLevel的定义:

    const UIWindowLevel UIWindowLevelNormal;    const UIWindowLevel UIWindowLevelAlert;    const UIWindowLevel UIWindowLevelStatusBar;    typedef CGFloat UIWindowLevel;

  IOS系统中定义了三个window层级,其中每一个层级又可以分好多子层级(从UIWindow的头文件中可以看到成员变量CGFloat _windowSublevel;),不过系统并没有把则个属性开出来。UIWindow的默认级别是UIWindowLevelNormal,我们打印输出这三个level的值分别如下:

2012-03-27 22:46:08.752 UIViewSample[395:f803] Normal window level: 0.000000

2012-03-27 22:46:08.754 UIViewSample[395:f803] Alert window level: 2000.000000

2012-03-27 22:46:08.755 UIViewSample[395:f803] Status window level: 1000.000000

  这样印证了他们级别的高低顺序从小到大为Normal < StatusBar < Alert,下面请看小的测试代码:

 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 2 {
 3 // Override point for customization after application launch.
 4 _normalWindow = [[UIWindow alloc] initWithFrame:CGRectInset([UIScreen mainScreen].bounds, 10.0f, 10.0f)];
 5 _normalWindow.backgroundColor = [UIColor blueColor];
 6 _normalWindow.windowLevel = UIWindowLevelNormal;
 7 [_normalWindow makeKeyAndVisible];
 8
 9 _statusBarWindow = [[UIWindow alloc] initWithFrame:CGRectInset([UIScreen mainScreen].bounds, 20.0f, 20.0f)];
10 _statusBarWindow.backgroundColor = [UIColor yellowColor];
11 _statusBarWindow.windowLevel = UIWindowLevelStatusBar;
12 [_statusBarWindow makeKeyAndVisible];
13
14 _alertWindow = [[UIWindow alloc] initWithFrame:CGRectInset([UIScreen mainScreen].bounds, 40.0f, 40.0f)];
15 _alertWindow.backgroundColor = [UIColor redColor];
16 _alertWindow.windowLevel = UIWindowLevelAlert;
17 [_alertWindow makeKeyAndVisible];
18
19 NSLog(@"Normal Window Level : %f" ,UIWindowLevelNormal);
20 NSLog(@"Statusbar Window Level : %f",UIWindowLevelStatusBar);
21 NSLog(@"Alert Window Level : %f",UIWindowLevelAlert);
22
23 return YES;
24 }

总结: WindowLevel高的覆盖WindowLevel低的,若WindowLevel相同,则先创建的覆盖后创建的。

ps:

一、

(由于现在Xcode5新建工程时自动选择ARC,所以应当使normalWindow,statusBarWindos,alertWindos 为AppDelegate的属性或成员变量,不然当didFinishLaunchingWithOptions

执行完,相应的局部变量windows也释放了,并不会在屏幕上显示出来。)

二、当你使用Stroyboard来设计你的UI界面时,你不需要明确的创建,配置和加载你的Window。系统默认为你做了三件事:

1.Instantiates a window.(实例化一个window)

2、Loads the main storyboard and instantiates its initial view controller.(加载main storyboard,并实例化其 initial View Controller)

3、Assigns the new view controller to the window’s rootViewController property and then makes the window visible.(将2中实例化的View Controller 赋于 1中window的rootViewController属性)

时间: 2024-08-01 02:38:18

(转摘+修改) 深入理解 UIWindow & UIWindowLevel的相关文章

(转载+ 修改) 深入理解UIWindow

转载于  http://www.cnblogs.com/smileEvday/archive/2012/11/16/UIWindow.html 每一个IOS程序都有一个UIWindow,在我们通过模板简历工程的时候,xcode会自动帮我们生成一个window,然后让它变成keyWindow并显示出来.这一切都来的那么自然,以至于我们大部分时候都忽略了自己也是可以创建UIWindow对象. 通常在我们需要自定义UIAlertView的时候(IOS 5.0以前AlertView的背景样式等都不能换)

UIWindow &amp; UIWindowLevel详解

一.UIWindow是一种特殊的UIView,通常在一个程序中只会有一个UIWindow,但可以手动创建多个UIWindow,同时加到程序里面.UIWindow在程序中主要起到三个作用: 1.作为容器,包含app所要显示的所有视图 2.传递触摸消息到程序中view和其他对象 3.与UIViewController协同工作,方便完成设备方向旋转的支持 二.通常我们可以采取两种方法将view添加到UIWindow中: 1.addSubview 直接将view通过addSubview方式添加到wind

争议的编程观点(网摘)

1.   业余时间不会为了好玩而编程的程序员,永远比不上那些以编程为乐的同学. 我认为即使是最聪明.最有才华的人,如果只是将编程作为工作,也永远成不了真正优秀的程序员.以编程为乐的人会在业余时也搞些小项目,或者弄弄各种不同的编程语言和编程思想. 2.   单元测试无助于编写优秀代码. 编写单元测试的唯一理由仅仅是确保已经能工作的代码不会出问题.先写测试或者按测试来写代码是无比荒谬的.如果在代码之前写测试,你都不知道边 界情况是什么.虽然能让代码通过测试,但是在没有预见到的情况时还是会出问题.而且

iOS 关于UIWindow 的认识

UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow iOS程序启动完毕后,创建的第一个视图控件就是UIWindow,接着创建控制器的view,最后将控制器的view添加到UIWindow上,于是控制器的view就显示在屏幕上了 一个iOS程序之所以能显示到屏幕上,完全是因为它有UIWindow.也就说,没有UIWindow,就看不见任何UI界面 如何获取UIWindow (1)[UIApplication sharedApplication].windows

UI-Day01--UIWindow &amp; UIWindowLevel(四)

UIWindow & UIWindowLevel详解 一.UIWindow是一种特殊的UIView,通常在一个程序中只会有一个UIWindow,但可以手动创建多个UIWindow,同时加到程序里面.UIWindow在程序中主要起到三个作用: 1.作为容器,包含app所要显示的所有视图 2.传递触摸消息到程序中view和其他对象 3.与UIViewController协同工作,方便完成设备方向旋转的支持 二.通常我们可以采取两种方法将view添加到UIWindow中: 1.addSubview 直

理解 bashrc 和 profile(转)

转自:https://wido.me/sunteya/understand-bashrc-and-profile/ 在一般的 linux 或者 unix 系统中, 都可以通过编辑 bashrc 和 profile 来设置用户的工作环境, 很多文章对于 profile 和 bashrc 也都有使用, 但究竟每个文件都有什么作用和该如何使用呢? 首先我们来看系统中的这些文件, 一般的系统可能会有 1 2 3 4 5 /etc/profile /etc/bashrc ~/.bashrc ~/.prof

Java基础3:深入理解String及包装类

Java基础3:深入理解String及包装类 String的连接 @Testpublic void contact () {    //1连接方式    String s1 = "a";    String s2 = "a";    String s3 = "a" + s2;    String s4 = "a" + "a";    String s5 = s1 + s2;    //表达式只有常量时,编译

dsu on tree:关于一类无修改询问子树可合并问题

dsu on tree:关于一类无修改询问子树可合并问题 开始学长讲课的时候听懂了但是后来忘掉了....最近又重新学了一遍 所谓\(dsu\ on\ tree\)就是处理本文标题:无修改询问子树可合并问题. \(dsu\)是并查集,\(dsu\ on\ tree\)是树上启发式合并,基于树剖(轻重链剖分). 无修改好理解,询问子树也好理解,啥是可合并啊? 举个简单的例子,集合的\(gcd\)就是可以合并的,就是两个集合\(gcd\)的\(gcd\):桶也是能合并的,对应位置相加就好了,诸如此类.

iOS面试必备-iOS基础知识

近期为准备找工作面试,在网络上搜集了这些题,以备面试之用. 插一条广告:本人求职,2016级应届毕业生,有开发经验.可独立开发,低薪求职.QQ:895193543 1.简述OC中内存管理机制. 答:内存管理机制:使用引用计数管理,分为ARC和MRC,MRC需要程序员自己管理内存,ARC则不需要.但是并不是 所有对象在ARC环境下均不需要管理内存,子线程和循环引用并不是这样.与retain配对使用的是release,retain代表引用计 数+1,release代表引用计数-1,当引用计数减为0时