1、delegate
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
2、windows
获取当前最顶层的窗口
UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
在实际需求中,讲某一UIView控件添加到最上层,可以考虑获取当前顶层的窗口来添加UIView控件,代码如下:
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 300)]; aView.backgroundColor = [UIColor redColor]; // 当前顶层窗口 UIWindow *window = [[UIApplication sharedApplication].windows lastObject]; // 添加到窗口 [window addSubview:aView];
仅做记录!