1,UIWindow的作用(继承UIView)
- 1.作为UIView的最顶层容器,包含应用显示所有的UIView;
- 2.传递触摸消息和键盘事件给UIView;
2,UIWindow的层级
1,UIWindow的层级由一个UIWindowLevel类型属性windowLevel,该属性指示了UIWindow的层级,windowLevel有三种可取值
(UIWindowLevelNormal; //默认,值为0;UIWindowLevelStatusBar ; // 值为1000;UIWindowLevelAlert; //值为2000 )
2,常用默认层级
- keyWindow的windowLevel为0;
- UITextEffectsWindow的windowLevel为1(iOS8新出,用来显示键盘顶部辅助视图);
- alertView为1996,比2000小点;
- sheetAction为2001,比2000大点;
- 键盘的UIRemoteKeyboardWindow,总是最大的,用来显示键盘按钮;
3,自定义UIWindow
自定义window,可以用来设定windowLevel,以适应显示需求
3,UIWindow的keyWindow的获取
UIWindow *window = [UIApplication sharedApplication].keyWindow;
4,UIWindow最外层window的获取
keywindow不一定是屏幕最前面的window,因为像UIAlert和键盘等,都是在屏幕最前面的window,或者是modal出来的控制器
UIWindow window = [[UIApplication sharedApplication].windows lastObject];
5,获取当前窗口显示的控制器
- (UIViewController *)getCurrentViewController
{
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
UIViewController *vc = keyWindow.rootViewController;
while (vc.presentedViewController)
{
vc = vc.presentedViewController;
if ([vc isKindOfClass:[UINavigationController class]])
{
vc = [(UINavigationController *)vc visibleViewController];
}
else if ([vc isKindOfClass:[UITabBarController class]])
{
vc = [(UITabBarController *)vc selectedViewController];
}
}
return vc;
}
6,获取当前屏幕中present出来的view controller
- (UIViewController *)getViewController
{
UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController *topVC = appRootVC;
if (topVC.presentedViewController) {
topVC = topVC.presentedViewController;
}
return topVC;
}