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

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

  1、addSubview

  2、rootViewController

三、WindowLevel

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

CGFloat _windowSublevel;),不过系统并没有把则个属性开出来。UIWindow的默认级别是UIWindowLevelNormal,我们打印输出这三个level的值分别如下:

46:08.752 UIViewSample[395:f803] Normal window level: 0.000000

  • 2012-03-27 22:46:08.754 UIViewSample[395:f803] Normal window level: 2000.000000
  • 2012-03-27 22:46:08.755 UIViewSample[395:f803] Normal window level: 1000.000000

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

TestWindowLevel

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor yellowColor];
[self.window makeKeyAndVisible];

UIWindow *normalWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
normalWindow.backgroundColor = [UIColor blueColor];
normalWindow.windowLevel = UIWindowLevelNormal;
[normalWindow makeKeyAndVisible];

CGRect windowRect = CGRectMake(50,
50,
[[UIScreen mainScreen] bounds].size.width - 100,
[[UIScreen mainScreen] bounds].size.height - 100);
UIWindow *alertLevelWindow = [[UIWindow alloc] initWithFrame:windowRect];
alertLevelWindow.windowLevel = UIWindowLevelAlert;
alertLevelWindow.backgroundColor = [UIColor redColor];
[alertLevelWindow makeKeyAndVisible];

UIWindow *statusLevelWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 50, 320, 20)];
statusLevelWindow.windowLevel = UIWindowLevelStatusBar;
statusLevelWindow.backgroundColor = [UIColor blackColor];
[statusLevelWindow makeKeyAndVisible];

NSLog(@"Normal window level: %f", UIWindowLevelNormal);
NSLog(@"Normal window level: %f", UIWindowLevelAlert);
NSLog(@"Normal window level: %f", UIWindowLevelStatusBar);

return YES;
}

  1)我们生成的normalWindow虽然是在第一个默认的window之后调用makeKeyAndVisible,但是仍然没有显示出来。这说明当Level层级相同的时候,只有第一个设置为KeyWindow的显示出来,后面同级的再设置KeyWindow也不会显示。

  2)statusLevelWindow在alertLevelWindow之后调用makeKeyAndVisible,淡仍然只是显示在alertLevelWindow的下方。这说明UIWindow在显示的时候是不管KeyWindow是谁,都是Level优先的,即Level最高的始终显示在最前面。


有时候,我们也希望在应用开发中,将某些界面覆盖在所有界面最上层。这个时候,我们就可以手工创建一个新的UIWindow。例如,想做一个密码保护功能,在用户从应用的任何界面按Home键退出,过段时间再从后台切换回来时,显示一个密码输入界面。

Demo界面很简单,每次启动应用或者从后台进入应用,都会显示输入密码界面,只有密码输入正确,才能使用应用。



大致代码如下:
PasswordInputWindow.h 文件。   定义一个继承自UIWindow的子类 PasswordInputWindow,  shareInstance 是单例, show方法就是用来显示输入密码界面。
@interface PasswordInputView : UIWindow

+ (PasswordInputView *)shareInstance;

- (void)show;

@end

PasswordInputWindow.m 文件。

#import "PasswordInputView.h"

@interface PasswordInputView()
@property (nonatomic,weak) UITextField *textField;
@end @implementation PasswordInputView #pragma mark - Singleton
+ (PasswordInputView *)shareInstance {
static id instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
}); return instance;
} #pragma mark - Initilize
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
} - (instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
[self setup];
}
return self;
} - (void)setup {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 20)];
label.text = @"请输入密码";
[self addSubview:label]; UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 80, 200, 20)];
textField.backgroundColor = [UIColor whiteColor];
textField.secureTextEntry = YES;
[self addSubview:textField];
self.textField = textField; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 110, 200, 44)];
[button setBackgroundColor:[UIColor blueColor]];
[button setTitle:@"确定" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(completeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button]; self.backgroundColor = [UIColor yellowColor];
} #pragma mark - Common Methods
- (void)completeButtonPressed:(UIButton *)button {
if ([self.textField.text isEqualToString:@"123456"]) {
[self.textField resignFirstResponder];
[self resignKeyWindow];
self.hidden = YES;
} else {
[self showErrorAlertView];
}
} - (void)showErrorAlertView {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"密码错误,请重新输入" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
} - (void)show {
[self makeKeyWindow];
self.hidden = NO;
} @end

1.代码中我实现了initWithFrame和initWithCoder两个方法,这样可以保证,不管用户是纯代码还是xib实现的初始化,都没有问题。

2.如果我们创建的UIWindow需
要处理键盘事件,那就要合理的将其设置为keyWindow。keyWindow是被系统设计用来接受键盘和其他非触摸事件的UIWindow。我们可以
通过makeKeyWindow 和 resignKeyWindow 方法来将自己创建的UIWindow实例设置成keyWindow。
3. 加入以下代码,就可以保证,程序每次从后台进入的时候,先显示输入密码界面了。

- (void)applicationDidBecomeActive:(UIApplication *)application {
[[PasswordInputView shareInstance] show];
}

UIWindow介绍的更多相关文章

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

    概述: UIWindow 类是 UIView 的子类,用于管理.协调应用中显示的窗口,其两个最重要的职能就是 容器,给 view 提供展示的区域: 将事件(例如:点击事件.拖拉事件等)分发给 view ...

  2. Coding源码学习第二部分(FunctionIntroManager.m)

    接上篇.上篇有一个细节忘了写,在Coding_iOS-Info.plist 里面添加了一个key 是 Status bar is initially hidden  Value 是 YES,在appl ...

  3. iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍

    UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...

  4. iOS开发UI篇—UIWindow简单介绍

    iOS开发UI篇—UIWindow简单介绍 一.简单介绍 UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow iOS程序启动完毕后,创建的第一个视图控件就是UIWi ...

  5. 86、UIWindow简单介绍

    一.介绍 UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow ios程序启动完毕后,创建的第一个视图控制器 ,接着创建控制器的view,最后将控制器的view添加到 ...

  6. OS开发UI篇—UIWindow简单介绍

    一.简单介绍 UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow iOS程序启动完毕后,创建的第一个视图控件就是UIWindow,接着创建控制器的view,最后将控 ...

  7. UIKit中的几个核心对象的介绍:UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍

    UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...

  8. iOS-UIViewController创建的几种方法和UIWindow的介绍

    在上一篇笔记中<iOS-程序启动原理和UIApplication>,http://blog.csdn.net/yang198907/article/details/49735531 在程序 ...

  9. iOS-iOS开发简单介绍

    概览 终于到了真正接触IOS应用程序的时刻了,之前我们花了很多时间去讨论C语言.ObjC等知识,对于很多朋友而言开发IOS第一天就想直接看到成果,看到可以运行的IOS程序.但是这里我想强调一下,前面的 ...

随机推荐

  1. python中使用正则表达式处理文本(仅记录常用方法和参数)

    标准库模块 python中通过re模块使用正则表达式 import re 常用方法 生成正则表达式对象 compile(pattern[,flags]) pattern:正则表达式字符串 flags: ...

  2. ServerBootstrap的handler()和childHandler()区别

    无论服务端还是客户端都进行了handler的设置,通过添加hanlder,我们可以监听Channel的各种动作以及状态的改变,包括连接,绑定,接收消息等. 区别: 1. handler在初始化时就会执 ...

  3. CF30E. Tricky and Clever Password

    被你谷翻译诈骗了兄弟. 不过下次可以拿去诈骗其他人. 考虑枚举B,显然结论有B作为回文串越长越好,这个可以使用manacher,或者直接二分hash. 然后考虑翻转末尾串,然后记录其匹配到第 \(i\ ...

  4. 入坑 OI 249561092 周年之际的一些感想

    2018.2.10~2021.2.10 又是一年的 2 月 10 日,今天的到来意味着我 OI 生涯的第三年已经结束,即将开启 OI 生涯的第四年了.回顾这三年以来自己由懵懂.无知慢慢变成熟的历程,感 ...

  5. 【 [SCOI2016]幸运数字】

    P3292 [SCOI2016]幸运数字 想法 倍增加上线性基就行惹 线性基的合并可以通过把一个线性基的元素插入到另一个里实现 #include<iostream> #include< ...

  6. 模版 动态 dp

    模版 动态 dp 终于来写这个东西了.. LG 模版:给定 n 个点的数,点有点权, $ m $ 次修改点权,求修改完后这个树的最大独立集大小. 我们先来考虑朴素的最大独立集的 dp \[dp[u][ ...

  7. R包xlsx安装与使用

     1. Rstudio安装xlsx报错 xlsx包加载依赖Java环境,我之前就安装过Java,但安装xlsx成功后,加载xlsx时一直报错: Error : loadNamespace()里算'rJ ...

  8. PHP非对称加密-RSA

    对称加密算法是在加密和解密时使用同一个密钥.与对称加密算法不同,非对称加密算法需要两个密钥--公开密钥(public key)和私有密钥(private key)进行加密和解密.公钥和密钥是一对,如果 ...

  9. R语言与医学统计图形【1】par函数

    张铁军,陈兴栋等 著 R语言基础绘图系统 基础绘图包之高级绘图函数--par函数 基础绘图包并非指单独某个包,而是由几个R包联合起来的一个联盟,比如graphics.grDevices等. 掌握par ...

  10. linux下vi与vim区别以及vim的使用-------vim编辑时脚本高光显示语法

    vi与vimvi编辑器是所有Unix及Linux系统下标准的编辑器,他就相当于windows系统中的记事本一样,它的强大不逊色于任何最新的文本编辑器.他是我们使用Linux系统不能缺少的工具.由于对U ...