UIWindow 介绍1:概述、作用、主要属性及方法

概述:

UIWindow 类是 UIView 的子类,用于管理、协调应用中显示的窗口,其两个最重要的职能就是

  1. 容器,给 view 提供展示的区域;
  2. 将事件(例如:点击事件、拖拉事件等)分发给 view。

而在视图等级中,UIWindow 就是视图等级中的根 view。而每一个 UIWindow 都有一个窗口等级(属性名:windowLevel),如果应用中同时出现了多个 UIWindow,那么窗口等级高的 UIWindow 将出现在窗口等级低的 UIWindow 的上面;例如:UIAlertView 会显示在一般窗口的上面,那是因为 UIAlertView 的窗口等级比一般窗口的窗口等级高。而一般情况下,一个 iOS 应用都只有一个 UIWindow。

而当你使用 Interface Builder 来创建 UIWindow 时,建议在属性检查器(Attributes inspector)中的载入选项(Launch option)中启用全屏选项,而如果全屏选项没开启的话,你创建的这个 UIWindow 的尺寸大小跟目标设备的尺寸大小是不一致的,这时可能会遇到以下两个问题:

  1. UIWindow 将无法接收到在 UIWindow 边界(bound)外产生的触摸事件(touch events);
  2. view 默认不会根据 UIWindow 的尺寸进行裁剪,导致 view 超出 UIWindow 的边界。

而在载入选项中启用全屏选项,可避免上述的两个问题。

UIWindow的作用

iOS 应用程序通常只有一个窗口,表示为一个 UIWindow 类的实例。应用程序在启动时创建这个窗口(或者从 nib 文件进行装载),并往窗口中加入一或多个视图,然后将它显示出来。窗口显示出来之后,很少需要再次引用它。

在 iOS 中,窗口对象并没有像关闭框或标题栏这样的视觉装饰,用户不能直接对其进行关闭或其它操作。所有对窗口的操作都需要通过其编程接口来实现。应用程序可以借助窗口对象来进行事件传递。窗口对象会持续跟踪当前的第一响应者对象,并在 UIApplication 对象提出请求时将事件传递它。

在 iOS 中,UIWindow 的父类是 UIView。因此,窗口在 iOS 中也是一个视图对象,但是一般情况下不必直接操作 UIWindow 对象中与视图有关的属性。

在创建应用程序窗口时,应该总是将其初始的边框尺寸设置为整个屏幕的大小。如果窗口是从 nib 文件装载得到,Interface Builder 并不允许创建比屏幕尺寸小的窗口;然而,如果你是通过编程方式创建的窗口,则必须在创建时传入期望的边框矩形。除了屏幕矩形之外,没有理由传入其它边框矩形。屏幕矩形可以通过 UIScreen 对象来取得,具体代码如下所示:

UIWindow* aWindow = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

 

主要属性:

  • keyWindow : BOOL 类型,只读,用于判断是否是当前应用的 key window (key window 是指可接收到键盘输入及其他非触摸事件的 UIWindow,一次只能有一个 key window)
  • rootViewController :UIViewController 类型,iOS 4.0新增属性,设置此属性后,rootViewController 所包含的 view 将被添加到 UIWindow 中,iOS 4.0之前版本可采用 addSubview: 方法达到同样的效果,因为 UIWindow 类本身就是 UIView 的子类。
  • windowLevel :UIWindowLevel 类型,多个 UIWindow 的显示顺序是按照 windowLevel 从高到低排列的,windowLevel 最高的显示在最前面。比如:windowLevel 较高的 UIAlertView 警告窗口会显示在一般窗口的前面。UIWindowLevel 类型的定义如下:

const UIWindowLevel UIWindowLevelNormal;   // 默认等级
const UIWindowLevel UIWindowLevelAlert;       // UIAlertView 的等级
const UIWindowLevel UIWindowLevelStatusBar;   // 状态栏的等级
typedef CGFloat UIWindowLevel;

从 UIWindowLevel 的定义可知,windowLevel 的值其实是一个 CGFloat 类型,我们将系统预设的三个等级打印出来,他们的值如下:

UIWindowLevelNormal:0.000000
UIWindowLevelAlert:2000.000000
UIWindowLevelStatusBar:1000.000000

UIWindowLevelNormal < UIWindowLevelStatusBar < UIWindowLevelAlert
而当你需要一个额外的 UIWindow,并且此 UIWindow 需要显示在 UIAlertView 的前面,可以将此 windowLevel 设置为 :UIWindowLevelAlert + 1,那么此 UIWindow 与 UIAlertView 同时显示时,此 UIWindow 将显示在 UIAlertView 前面。

我们知道了多个不同 windowLevel 的 UIWindow 会按照 windowLevel 从高到低进行显示排序,那如果相同 windowLevel 的多个 UIWindow 又会怎么显示呢?这个问题后面的例子中会解答。

 

主要方法:

  • makeKeyWindow :将当前 UIWindow 设置成应用的 key window。
  • makeKeyAndVisible :将当前 UIWindow 设置成应用的 key window,并使得 UIWindow 可见;你也可以使用 UIView 的 hidden 属性来显示或者隐藏一个 UIWindow。

 

相同 windowLevel 等级 UIWindow 显示例子

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- ( BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
     self.window.backgroundColor = [UIColor whiteColor];
     [self.window makeKeyAndVisible];
     UIWindow *windowNormal = [[UIWindow alloc] initWithFrame:CGRectMake(20, 20, 100, 200)];
     windowNormal.backgroundColor = [UIColor redColor];
     windowNormal.windowLevel = UIWindowLevelNormal + 1;
     [windowNormal makeKeyAndVisible];
     UIWindow *windowNormal2 = [[UIWindow alloc] initWithFrame:CGRectMake(40, 40, 100, 200)];
     windowNormal2.backgroundColor = [UIColor blueColor];
     windowNormal2.windowLevel = UIWindowLevelNormal + 1;
     [windowNormal2 makeKeyAndVisible];
     return  YES;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值