ios – 如何在UIWindow中添加视图?

前端之家收集整理的这篇文章主要介绍了ios – 如何在UIWindow中添加视图?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在UIWindow中添加一个视图,其中包含以下代码
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
 UIWindow *window = delegate.window;
 UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0,100,100)];
 aView.backgroundColor = [UIColor blackColor];
 [window addSubview:aView];

这段代码不起作用.我想克隆UIAlertView的属性.当我们打电话时,它会弹出一切
[alertViewInstance show];方法.

试过这个:

UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0,100)];
    UIWindow* window = [UIApplication sharedApplication].keyWindow;

    if (!window) {
        window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    }

    [window addSubview:aView];
    [window bringSubviewToFront:aView];

解决方法

试试这段代码
window = [[UIApplication sharedApplication].windows lastObject];

替换以下代码

window = [[UIApplication sharedApplication].windows objectAtIndex:0];
原文链接:https://www.f2er.com/iOS/333704.html

猜你在找的iOS相关文章