How to add a view to UIWindow?

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

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

This code does not work. I wanted to clone a UIAlertView property. It will pop up above everything when we call [alertViewInstance show]; .

Tried this as well:

  UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; UIWindow* window = [UIApplication sharedApplication].keyWindow; if (!window) { window = [[UIApplication sharedApplication].windows objectAtIndex:0]; } [window addSubview:aView]; [window bringSubviewToFront:aView]; 
+6
source share
8 answers

Try this code:

 window = [[UIApplication sharedApplication].windows lastObject]; 

Replace the following code:

 window = [[UIApplication sharedApplication].windows objectAtIndex:0]; 
+6
source

Try using 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]; } 
+5
source

If you are using the UINavigationController Use:

 [self.navigationController.view.window addSubview:aView]; 

If you are using UITabBarController Use:

 [self.tabBarController.view.window addSubview:aView]; 

In AppDelegate you can directly assign a viewport. In the AppDelegate method didFinishLaunchingWithOptions Usage:

 [self.window addSubview:aView]; 

Hope this helps ...

+4
source

Your windows should 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]; 
+3
source

You can add a view using the following

 [[[UIApplication sharedApplication] keyWindow] addSubview:YOUR_VIEW]; 
+3
source

You can try under the code ... to add a subview to the window

 UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; aView.backgroundColor = [UIColor blackColor]; [[UIApplication sharedApplication].keyWindow addSubview:aView]; 

// Delete

 [aView removeFromSuperview]; 

Hope this helps you! ..

+2
source

I think you are missing it. check once.

 [self.window makeKeyAndVisible]; 
+2
source

UIAlertView creates another UIWindow and sets this window as a key window when the show method is called. Therefore, if you want to show your own alert view, create an instance of UIWindow and add your custom alert view to this newly created window.

+1
source

Source: https://habr.com/ru/post/984035/


All Articles