I wanted to add a view in UIWindow
with following code:
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
UIWindo
If are you using UINavigationController
Use:
[self.navigationController.view.window addSubview:aView];
If are you using UITabBarController
Use:
[self.tabBarController.view.window addSubview:aView];
In AppDelegate
you can directly assign a view to window. In appDelegate
didFinishLaunchingWithOptions
method Use:
[self.window addSubview:aView];
Hope it helps...
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];
UIAlertView creates another UIWindow and set this window as key window when you call show method. So if you want to show your own alert view create a UIWindow instance show it and add your custom alert view to this newly created window.
You can add view using the following
[[[UIApplication sharedApplication] keyWindow] addSubview:YOUR_VIEW];
Try this code:
window = [[UIApplication sharedApplication].windows lastObject];
Replace the following code:
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
You can try below code...for adding subview to window
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
aView.backgroundColor = [UIColor blackColor];
[[UIApplication sharedApplication].keyWindow addSubview:aView];
//Removing
[aView removeFromSuperview];
Hope it helps you..!