自定义创建Window
self.window1 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 400, 150, 150)];
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor redColor];
self.window1.rootViewController = vc1;
self.window1.windowLevel = 9;
[self.window1 makeKeyAndVisible];
- 注意: [window makeKeyWindow];
window.hidden = NO;
等价与
[self.window2 makeKeyAndVisible];
UIApplication.sharedApplication.delegate.window和UIApplication.sharedApplication.keyWindow有什么区别?
- [UIApplication sharedApplication].keyWindow
是设备上当前正在显示的窗口。这通常是您应用程序的窗口,但也可能是系统窗口 - [UIApplication sharedApplication].delegate.window
是AppDelegate中定义的属性窗口,是主window;
@property (strong, nonatomic) UIWindow *window; - [UIApplication sharedApplication].windows.firstObject
等价于
[UIApplication sharedApplication].delegate.window - [UIApplication sharedApplication].windows.lastObject
- [[UIApplication sharedApplication]windows].lastObject
用来获取当前屏幕上最上层的一个UIWindow,但有可能不是当前的window
- (void)alertView {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"测试" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
[alertView show];
// UIWindow *window = [UIApplication sharedApplication].keyWindow; //是上面alert的window
UIWindow *window = [UIApplication sharedApplication].delegate.window;// 是AppDelegate中定义的属性窗口
UILabel *tempLabel =[[UILabel alloc]initWithFrame:CGRectMake(150, 150, 100, 20)];
tempLabel.font = [UIFont systemFontOfSize:16];
tempLabel.backgroundColor = [UIColor purpleColor];
tempLabel.text = @"测试window";
[window addSubview:tempLabel];
}
- 注意:UIWindow *window = [UIApplication sharedApplication].keyWindow,如果使用这个的时候,下面的tempLabel就会添加到UIAlertView所在的_UIAlertControllerShimPresenterWindow层,当alert消失的时候,对应的windows和tempLabel都会释放
窗口的定义:
https://developer.apple.com/documentation/uikit/uiwindow?language=objc
网友评论