#import@interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window;//最顶层界面容器@end#import "AppDelegate.h"#import "PassWordInputWindow.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch.//// self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];// self.window.rootViewController = [self rootViewController];// [self.window makeKeyAndVisible]; // UIWindow 主要作用:// 1,作为UIView的最顶层容器,包含应用显示所需要的所有UIView// 2,传递触摸消息和键盘事件给UIView // 添加UIView的方式// self.window addSubview:<#(UIView *)#>// self.window.rootViewController return YES;}- (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. // home退出后,需加载重新输入密码(解锁)界面 [[PassWordInputWindow sharedInstance] show];}- (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end#import "ViewController.h"UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal;UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert;UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar;@interface ViewController (){ UIButton *btn; // 手工创建UIWindow UIWindow *uiwindow; UIButton *btnWindow;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; // 系统对UIWindow的使用 btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; btn.backgroundColor = [UIColor orangeColor]; [self.view addSubview:btn]; [btn setTitle:@"textAlertView" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(testAlertView) forControlEvents:UIControlEventTouchUpInside]; btnWindow = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 100)]; btnWindow.backgroundColor = [UIColor orangeColor]; [self.view addSubview:btnWindow]; [btnWindow setTitle:@"textAlertView" forState:UIControlStateNormal]; [btnWindow addTarget:self action:@selector(creatWindow) forControlEvents:UIControlEventTouchUpInside]; // 打印层级 NSLog(@"UIWindowLevelNormal%f UIWindowLevelAlert%f UIWindowLevelStatusBar%f",UIWindowLevelNormal,UIWindowLevelAlert,UIWindowLevelStatusBar);// UIWindow使用[9632:1292346] UIWindowLevelNormal0.000000 UIWindowLevelAlert2000.000000 UIWindowLevelStatusBar1000.000000}-(void)testAlertView{ UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"text" message:@"UIWindow" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil]; [alert show]; }//可以用在启动广告 ,重新登录,应用内弹窗广告-(void)creatWindow{ uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; uiwindow.windowLevel = UIWindowLevelNormal; uiwindow.backgroundColor = [UIColor greenColor]; uiwindow.hidden = NO; UIGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]init]; [gesture addTarget:self action:@selector(hideWindow:)]; [uiwindow addGestureRecognizer:gesture]; }-(void)hideWindow:(UIGestureRecognizer*) gesture{ uiwindow.hidden = YES; uiwindow = nil; NSLog(@"隐藏window");}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#import @interface PassWordInputWindow : UIWindow+(PassWordInputWindow *)sharedInstance;-(void)show;@end#import "PassWordInputWindow.h"@implementation PassWordInputWindow{ UITextField *_textField;}+(PassWordInputWindow *)sharedInstance{ static id sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken,^{ sharedInstance = [[self alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; }); return sharedInstance;}-(id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self){ UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 80, 200, 20)]; label.text = @"请输入密码"; [self addSubview:label]; UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 110, 200, 20)]; textField.backgroundColor = [UIColor whiteColor]; textField.secureTextEntry = YES; [self addSubview:textField]; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 140, 200, 44)]; [button setBackgroundColor:[UIColor blueColor]]; button.titleLabel.textColor = [UIColor blackColor]; [button setTitle:@"确定" forState:UIControlStateNormal]; [button addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; self.backgroundColor = [UIColor yellowColor]; _textField = textField; } return self;}-(void)show{ [self makeKeyWindow]; self.hidden = NO; }-(void)ButtonPressed{ if ([_textField.text isEqualToString:@"abcde"]){ [_textField resignFirstResponder]; [self resignKeyWindow]; self.hidden = YES; }else { [self showErrorAlertView]; }}-(void)showErrorAlertView{ UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"密码错误,请输入正确密码(abcde)" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show];}@end