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];
}

最新文章

  1. CoreCRM 开发实录 —— Profile
  2. CREATE A LOADING SCENE / SPLASH SCREEN - UNITY
  3. python scrapy 获取华为应用市场APP评论数据
  4. 在IIS7.5打开网页的时候,提示: HTTP 错误 500.0 - Internal Server Error 调用 LoadLibraryEx 失败,在 ISAPI 筛选器 &quot;C:\Windows\Microsoft.NET\Framework\v4.0.30319\\aspnet_filter.dll&quot; 上。解决方法
  5. .NET自带IOC容器MEF之初体验
  6. ArcGIS Server JavaScript API 各命名空间的含义【转】
  7. ADB server didn&#39;t ACK的问题
  8. Swift - 创建代理协议实现页面间参数传递和方法调用
  9. android中自定义Theme以及TitleBar
  10. MSCI 成份股 清单
  11. Java 标准 I/O 介绍
  12. 自学传说中的php接口编写
  13. 国内写的比较好的markdown教程
  14. 利用salt搭建hadoop集群
  15. Python模块——logging模块
  16. Zabbix客户端(被监控端)安装配置
  17. 封装QML能访问的类
  18. python零散补充与总结
  19. zw版【转发&#183;台湾nvp系列Delphi例程】HALCON AngleLl
  20. 剑指offer-特定二维数组中查找一个元素是否存在-二分搜索-二维数组

热门文章

  1. python读写文件with open
  2. oracle 创建空间索引
  3. rocketmq 精华
  4. LOJ #2185 / 洛谷 P3329 - [SDOI2015]约数个数和(莫比乌斯函数)
  5. excel-合并多个Excel文件--VBA合并当前目录下所有Excel工作簿中的所有工作表
  6. mysql 不等于 符号写法
  7. 构建LNMP+WordPress
  8. A Child&#39;s History of England.48
  9. Spark(八)【利用广播小表实现join避免Shuffle】
  10. celery开启worker报错django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE o