ios开发之--找window,获取屏幕最上层的窗口

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];

 

仅做记录!

posted @ 2020-02-11 15:05  稻草人11223  阅读(1618)  评论(0编辑  收藏  举报
返回顶部