UIWindow的坑

2021-09-22  本文已影响0人  小_梦

自定义创建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];

UIApplication.sharedApplication.delegate.window和UIApplication.sharedApplication.keyWindow有什么区别?

  1. [UIApplication sharedApplication].keyWindow
    是设备上当前正在显示的窗口。这通常是您应用程序的窗口,但也可能是系统窗口
  2. [UIApplication sharedApplication].delegate.window
    是AppDelegate中定义的属性窗口,是主window;
    @property (strong, nonatomic) UIWindow *window;
  3. [UIApplication sharedApplication].windows.firstObject
    等价于
    [UIApplication sharedApplication].delegate.window
  4. [UIApplication sharedApplication].windows.lastObject
  5. [[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];
    
}

窗口的定义:

https://developer.apple.com/documentation/uikit/uiwindow?language=objc

上一篇下一篇

猜你喜欢

热点阅读