vicki753's iOS 基础--UIWindow

2017-12-29  本文已影响93人  vicki753

做项目以来一直以为UIWindow就只是简单的表面一层视图,就是可以接收到所有的手势的一种视图,但实际上它还有很多属性和种类:

在Textfield或者TextView 时显示了键盘的时候

textfield 还没有被触发编辑,键盘还没有被弹起时,这时的Window只有

UIWindow

subView:

textfield触发编辑,keyboard弹起时,

UIRemoteKeyboardWindow

subVies:

UITextEffectsWindow

subViews:

UIWindow

subViews:

进一步解析

自己写的一点源码

+ (UIWindow *)frontWindow {
NSLog(@"windows array %@",[UIApplication.sharedApplication windows]);
NSEnumerator *frontToBackWindows = [UIApplication.sharedApplication.windows reverseObjectEnumerator];
for (UIWindow *window in frontToBackWindows) {
    NSLog(@"Enum %s %p %@", object_getClassName(window), &window, window);
    if (strstr(object_getClassName(window), "UIRemoteKeyboardWindow")) {
        
        [self printWindowDetialInfoWithWindow:window andBackgroundColor:[UIColor redColor]];
        
        for (UIView *subViewOfKeyBoard in [window subviews]) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 700, 100, 40)];
            view.backgroundColor = [UIColor purpleColor];
            [subViewOfKeyBoard addSubview:view];
        }
    }else if (strstr(object_getClassName(window), "UITextEffectsWindow")){
        [self printWindowDetialInfoWithWindow:window andBackgroundColor:[UIColor yellowColor]];
    }else if (strstr(object_getClassName(window), "UIWindow")) {
        [self printWindowDetialInfoWithWindow:window andBackgroundColor:[UIColor grayColor]];
    }
    BOOL windowOnMainScreen = window.screen == UIScreen.mainScreen;
    BOOL windowIsVisible = !window.hidden && window.alpha > 0;
    BOOL windowLevelSupported = (window.windowLevel == UIWindowLevelNormal);
    
    if(windowOnMainScreen && windowIsVisible && windowLevelSupported) {
        return window;
    }
}
return nil;
}

+(void)printWindowDetialInfoWithWindow:(UIWindow *)window andBackgroundColor:(UIColor *)color{
window.backgroundColor = color;
NSLog(@"find the %s %p", object_getClassName(window), &window);
NSLog(@"window level = %lf", window.windowLevel);//window level = 1.000000 , window level string = bar
NSLog(@"window level string = %@", window.windowLevel == UIWindowLevelNormal ? @"normal" : (window.windowLevel == UIWindowLevelAlert ? @"alert" : @"bar"));
for (UIView *subViewOfTextEffectsWindow in [window subviews]) {
    NSLog(@"%s subView %@", object_getClassName(window) ,subViewOfTextEffectsWindow);
}
}

清楚UIWindow出现的多个的情况以及它们的优先级:

这个方法返回的是最front window对象。其实是最‘老大’的Window,并不是所谓的最前面,因为一切的touch手势其实都是从这里接收的。

这段代码细究一下window.level:

UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal;
UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert;
UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar   __TVOS_PROHIBITED;

这个时windowLevel的官方API解释
@property(nonatomic) UIWindowLevel windowLevel;
Description
The position of the window in the z-axis.
Window levels provide a relative grouping of windows along the z-axis. All windows assigned to the same window level appear in front of (or behind) all windows assigned to a different window level. The ordering of windows within a given window level is not guaranteed.
The default value of this property is UIWindowLevelNormal. For a list of other possible window levels, see UIWindowLevel.
SDKs iOS 2.0+, tvOS 9.0+
Declared In UIKit
More Property Reference

看api的介绍就是表示在z轴上的窗口位置。

UIWindow的window level是0.000000
UITextEffectsWindow 的window level是1.000000
UIRemoteKeyboardWindow 的window level 是10000001.000000

所以,可以知道,UIRemoteKeyboardWindow是何其的高?!!
但是你依然可以在UIRemoteKeyboardWindow上面添加SubView,
这个UIRemoteKeyboardWindow也是单例,所以可以在其他的视图控制器获取到。
(如果想知道单例有哪些,可以看这个:http://www.jianshu.com/p/8a5aa5e7b6e0

如果使用了UIAlertController

如果使用了UIAlertController,那么它的层级又是什么样子呢?

如果只是简单的文字描述,没有textfield,也就是没有文字输入,没有键盘的情况下是只有两层window:

UITextEffectsWindow

它的subViews:

UIWindow

它的subViews:

如果是add了Textfield,那么放心,键盘也会随着UIAlerController的到来而到来,所以,和第一个情况一样有三种Window:

UIRemoteKeyboardWindow

它的Subviews:
*一个 UIInputSetContainerView frame = (0 0; 414 736)

UITextEffectsWindow

它的SubViews:

UIWindow

它的SubViews:

还不够详细,后续研究,多多指教🐵

上一篇 下一篇

猜你喜欢

热点阅读