UIKit-UIWindow详解
(文章转载自:http://www.jianshu.com/p/2d4dcf7d97eb)
UIKit-UIWindow详解

UIWindow继承图
属性
1.screen (window的屏幕)
/*
* window的屏幕,默认是 [UIScreen mainScreen] ,不能更改,否则没有界面
*/
@property(nonatomic,retain) UIScreen *screen NS_AVAILABLE_IOS(3_2);
2.windowLevel (window层级)
/*
* window的视图层级,默认是0.0
*/
@property(nonatomic) UIWindowLevel windowLevel;
typedef CGFloat UIWindowLevel;
通过 typedef 将 CGFloat 转义成 UIWindowLevel
windowLevel 属性的类型为 UIWindowLevel , 其实就是 CGFloat ,只是转义一下更易于理解
windowLevel有以下三种:
- UIWindowLevelNormal;// 0.000000
- UIWindowLevelStatusBar;// 1000.000000
- UIWindowLevelAlert;// 2000.000000
3.keyWindow (是否是keyWindow)
@property(nonatomic,readonly,getter=isKeyWindow) BOOL keyWindow
4.rootViewController (根控制器)
/*
* window的根控制器,默认是nil
*/
@property(nonatomic,retain) UIViewController *rootViewController NS_AVAILABLE_IOS(4_0);
方法
- (void)becomeKeyWindow; // 不可直接调用
- (void)resignKeyWindow; // 不可直接调用
- (void)makeKeyWindow;
- (void)makeKeyAndVisible; // 大部分apps 通过调用这个方法显示主window并将window设为key,否则用视图的隐藏属性
- (void)sendEvent:(UIEvent *)event; // 通过UIApplication 调用来分发任务给window 中的视图
- (CGPoint)convertPoint:(CGPoint)point toWindow:(UIWindow *)window; // 把该window中的一个坐标转换成在目标window中时的坐标值
- (CGPoint)convertPoint:(CGPoint)point fromWindow:(UIWindow *)window; // 把目标window中的一个坐标转换成在该window中时的坐标值
- (CGRect)convertRect:(CGRect)rect toWindow:(UIWindow *)window; // 把该window中的一个矩阵转换成在目标window中时的矩阵值
- (CGRect)convertRect:(CGRect)rect fromWindow:(UIWindow *)window;// 把目标window中的一个矩阵转换成在该window中时的矩阵值
常量
监测window的通知:
UIKIT_EXTERN NSString *const UIWindowDidBecomeVisibleNotification; // 当window激活时并展示在界面的时候触发,返回空
UIKIT_EXTERN NSString *const UIWindowDidBecomeHiddenNotification; // 当window隐藏的时候触发,暂时没有实际测,返回空
UIKIT_EXTERN NSString *const UIWindowDidBecomeKeyNotification; // 当window被设置为keyWindow时触发,返回空
UIKIT_EXTERN NSString *const UIWindowDidResignKeyNotification; // 当window的key位置被取代时触发,返回空
监测键盘的通知:
UIKIT_EXTERN NSString *const UIKeyboardWillShowNotification;
UIKIT_EXTERN NSString *const UIKeyboardDidShowNotification;
UIKIT_EXTERN NSString *const UIKeyboardWillHideNotification;
UIKIT_EXTERN NSString *const UIKeyboardDidHideNotification;
UIKIT_EXTERN NSString *const UIKeyboardWillChangeFrameNotification NS_AVAILABLE_IOS(5_0);
UIKIT_EXTERN NSString *const UIKeyboardDidChangeFrameNotification NS_AVAILABLE_IOS(5_0);
实例
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/*
* 1 监测window
*/
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(windowDidBecomeVisible:) name:UIWindowDidBecomeVisibleNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(windowDidBecomeHidden:) name:UIWindowDidBecomeHiddenNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(windowDidBecomeKey:) name:UIWindowDidBecomeKeyNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(windowDidResignKey:) name:UIWindowDidResignKeyNotification object:nil];
/*
* 2 监测keyboard
*/
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidShowNotification:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidHideNotification:) name:UIKeyboardDidHideNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidChangeFrameNotification:) name:UIKeyboardDidChangeFrameNotification object:nil];
// window0
self.window0 = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
self.window0.backgroundColor = [UIColor purpleColor];
self.window0.windowLevel = UIWindowLevelNormal;
UITextField *tf0 = [[UITextField alloc]initWithFrame:CGRectMake(0, 20, 100, 20)];
tf0.layer.borderColor = [UIColor blueColor].CGColor;
tf0.layer.borderWidth = 1;
tf0.delegate = self;
[self.window0 addSubview:tf0];
UILabel *lab0 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 100, 20)];
lab0.text = @"视图一";
lab0.font = [UIFont boldSystemFontOfSize:18];
[self.window0 addSubview:lab0];
[self.window0 makeKeyAndVisible];
// window1
self.window1 = [[UIWindow alloc]initWithFrame:CGRectMake(100, 100, [UIScreen mainScreen].bounds.size.width - 100, [UIScreen mainScreen].bounds.size.height- 100)];
self.window1.backgroundColor = [UIColor brownColor];
self.window1.windowLevel = UIWindowLevelStatusBar;
UITextField *tf1 = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
tf1.layer.borderColor = [UIColor blueColor].CGColor;
tf1.layer.borderWidth = 1;
tf1.delegate = self;
[self.window1 addSubview:tf1];
UILabel *lab1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 100, 20)];
lab1.text = @"视图二";
lab1.font = [UIFont boldSystemFontOfSize:18];
[self.window1 addSubview:lab1];
[self.window1 makeKeyAndVisible];
// window2
self.window2 = [[UIWindow alloc] initWithFrame:CGRectMake(200, 200, [UIScreen mainScreen].bounds.size.width - 200, [UIScreen mainScreen].bounds.size.height- 200)];
self.window2.windowLevel = UIWindowLevelAlert;
self.window2.backgroundColor = [UIColor redColor];
UITextField *tf2 = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
tf2.layer.borderColor = [UIColor blueColor].CGColor;
tf2.layer.borderWidth = 1;
tf2.delegate = self;
[self.window2 addSubview:tf2];
UILabel *lab2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 100, 20)];
lab2.text = @"视图三";
lab2.font = [UIFont boldSystemFontOfSize:18];
[self.window2 addSubview:lab2];
[self.window2 makeKeyAndVisible];
CGPoint p = [self.window1 convertPoint:CGPointMake(0, 0) toWindow:self.window0];
NSLog(@"%@",NSStringFromCGPoint(p));
NSLog(@"-------------%@",[