iOS UIWindow

490 阅读1分钟

1.UIWindow的作用:

- 作为UIView的顶层容器,包含应用所需的全部view
- 传递触摸消息和键盘时间给UIView

2.UIWindow添加UIView

- UIWindow是UIView的子类,可以直接使用addsubview
- 设置其rootViewController

3.系统对UIWindow的使用

在使用UIAlertView时,系统会保证UIAlertView在所有界面之上,因此会临时创建一个新的UIWindow,将其UIWindowLevel设置更高,让UIAlertView覆盖所有界面之上

4.UIWindowLevel

UIWindowLevelNormal = 0.0000
UIWindowLevelAlert = 1000
UIWindowLevelStatusBar = 2000

5.手动创建UIWindow

适合场景:应用启动页应用内通知应用内弹框手势解锁等。

@interface PasswordInputWindow : UIWindow

    + (PasswordInputWindow *)sharedInstance;

    - (void)show;

@end
#import "PasswordInputWindow.h"

@implementation PasswordInputWindow

+ (PasswordInputWindow *)sharedInstance{
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    });
    return sharedInstance;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView{
    
}

- (void)show{
    [self makeKeyAndVisible];
    self.hidden = NO;
}

- (void)hidde{
    [self resignKeyWindow];
    self.hidden = YES;
}