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. win10的pycharm中安装ansible模块过程

    前面的安装报错信息 ansible模块安装报错:Could not install packages due to an OSError: [Errno 2] No such file or dire ...

  2. .Net Crank性能测试入门

    Crank 是微软新出的一个性能测试框架,集成了多种基准测试工具,如bombardier.wrk等. Crank通过统一的配置,可以转换成不同基准测试工具命令进行测试.可参考Bombardier Jo ...

  3. pm2 重启策略(restart strategies)

    使用 PM2 启动应用程序 时,应用程序会在自动退出.事件循环为空 (node.js) 或应用程序崩溃时自动重新启动. 但您也可以配置额外的重启策略,例如: 使用定时任务重新启动应用程序 文件更改后重 ...

  4. 带你了解Typescript的14个基础语法

    摘要:Typescript可以说是JavaScript的超集,在JS的基础上新增了许多语法特性,使得类型不再可以随意转换,能大大减少开发阶段的错误. 本文分享自华为云社区<Typescript基 ...

  5. 洛谷 P7360 -「JZOI-1」红包(Min-Max 容斥+推式子)

    洛谷题面传送门 hot tea. 首先注意到这个 \(\text{lcm}\) 特别棘手,并且这里的 \(k\) 大得离谱,我们也没办法直接枚举每个质因子的贡献来计算答案.不过考虑到如果我们把这里的 ...

  6. ARM汇编基础指令

    Cortex-A7 常用汇编指令 一.处理器内部数据传输指令 1.mov 将数据从一个寄存器拷贝到另外一个寄存器,或者将一个立即数传递到寄存器里面 MOV R0,R1 @将寄存器 R1 中的数据传递给 ...

  7. Vector总结及部分底层源码分析

    Vector总结及部分底层源码分析 1. Vector继承的抽象类和实现的接口 Vector类实现的接口 List接口:里面定义了List集合的基本接口,Vector进行了实现 RandomAcces ...

  8. 14 - springboot的@Configuration、@Bean、@Import()、@ImportResource()、@Conditional说明

    1.@Configuration.@Bean.@Import().@ImportResource().@Conditional 分析源码的时候总会见到标题中的这几个注解,因此:弄一篇博客来说明一下吧, ...

  9. 振鹏同学正式学习java的第一天!

    一.今日收获 1.最棒的莫过于运行Java的HelloWorld! 2.在同学的帮助下历经坎坷困苦安装完成了Eclipse软件并设置好环境变量. 3.最最最开始了解了Java的前世今生,编程语言发展的 ...

  10. Scala【需求二:求各省市的各个指标】

    需求处理步骤 原始数据->json->过滤->列裁剪 需求二:求各省市的各个指标 原始数据 文本pmt.json,每一行都是一个json字符串.里面包含ip等信息 {"se ...