How to add view in UIWindow?

前端 未结 8 1394
星月不相逢
星月不相逢 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:06

    Your windows needs to have a root view controller.

    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    UIWindow *window = delegate.window;
    UIViewController *controller = [[UIViewController alloc] init]; 
    window.rootViewController = controller;
    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    aView.backgroundColor = [UIColor blackColor];
    [controller.view addSubview:aView];
    

提交回复
热议问题