How to add view in UIWindow?

前端 未结 8 1403
星月不相逢
星月不相逢 2021-02-14 22:42

I wanted to add a view in UIWindow with following code:

 AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
 UIWindo         


        
8条回答
  •  北恋
    北恋 (楼主)
    2021-02-14 23:22

    Try with this code:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        UIWindow* window = [UIApplication sharedApplication].keyWindow;
        if (!window) {
            window = [[UIApplication sharedApplication].windows objectAtIndex:0];
        }
    
        UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        aView.backgroundColor = [UIColor redColor];
        aView.center = window.center;
        [window insertSubview:aView aboveSubview:self.view];
        [window bringSubviewToFront:aView];
    }
    

提交回复
热议问题