关于UIWindow,使用小记

UIWindow是可以自建很多的。默认初始只有一个window。

UIStatusbar 、 UIAlertView 、UIActionSheet 还有键盘,都属于UIWindow类。

通过 

[UIApplication sharedApplication].windows

可以打印出,当前APP中的所有window

"<UIWindow: 0xb831fe0; frame = (0 0; 320 568); layer = <UIWindowLayer: 0xb919670>>"

如上:是我默认的一个window。

当需要新加一个时,可以通过如下方式操作:

@property (nonatomic, strong) UIWindow *thisAlertWindow;


//这里我们使延迟初始化实例
- (UIWindow *)thisAlertWindow
{
    if (!_thisAlertWindow) {
        _thisAlertWindow = [[UIWindow alloc] initWithFrame:_win.bounds];
        [_thisAlertWindow setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.4]];
        /*
         UIWindowLevel 有如下三种:
         UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal;
         UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert;
         UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar;
         */
        //
        [_thisAlertWindow setWindowLevel:UIWindowLevelAlert + 1];//
        [_thisAlertWindow addSubview:self];
    }
    return _thisAlertWindow;
}

从上面可以看到UIWindowLevel有三种类型。

alert最上,statusbar中间,normal最下。

大家可以通过平时使用,UIAlertView如果弹出来的话是不可以操作界面的。这说明它的优先级很高。

这里很多我就不提供图片了,大家可以自行测试,UIAlertView 与 UIActionSheet 的优先级。

其实这两者同属于UIWindowLevelAlert,但是同等级的可以通过设置不同Integer 实现优先级不同。

如上面的实例。

但是上面的实例建好后,它并不能显示,需要通过如下代码:

[self.thisAlertWindow makeKeyAndVisible];//这样才可以显示出来

一旦创建window,它就可以常存在。但是有时候,我们只想在我们需要的时候要,不需要的时候就不要。

下面,就说下如何移除。

大家知道UINavgationController 可以通过 viewcontrollers得到所有的控制器。

同样,大家可以通过下面方法获取所有已添加的windows

[UIApplication sharedApplication].windows
但是,不同的是  UINavgationController得到的是一个可变数组,而上面方法得到的却是一个不可变数组。

所以,不能像UINavgationController那样直接通过移除数组成员来实现window的移除。

说远了。。。。

直接上方法:

[self.thisAlertWindow resignKeyWindow];
_thisAlertWindow = nil;

是不是看的有点像 UITextField的用法。

下面贴出 添加删除后的打印结果

添加后

//"<UIWindow: 0xb831fe0; frame = (0 0; 320 568); layer = <UIWindowLayer: 0xb919670>>",
//"<UIWindow: 0xa2e1f80; frame = (0 0; 320 568); layer = <UIWindowLayer: 0xa2dc5e0>>"

移除后:

//"<UIWindow: 0xb831fe0; frame = (0 0; 320 568); layer = <UIWindowLayer: 0xb919670>>"

更多的细节研究,以后有时间再整理吧。



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值