如何在UIWindow中添加视图?
我想在UIWindow
添加一个视图,其中包含以下代码:
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];
这段代码不起作用。 我想克隆UIAlertView的属性。 当我们调用[alertViewInstance show];
时,它会弹出所有内容[alertViewInstance show];
方法。
试过这个:
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];
试试这段代码:
window = [[UIApplication sharedApplication].windows lastObject];
替换以下代码:
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
试试这段代码:
- (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]; }
如果您使用的是UINavigationController
使用:
[self.navigationController.view.window addSubview:aView];
如果你使用UITabBarController
使用:
[self.tabBarController.view.window addSubview:aView];
在AppDelegate
您可以直接将视图分配给窗口。 在appDelegate
didFinishLaunchingWithOptions
方法使用:
[self.window addSubview:aView];
希望能帮助到你…
您的Windows需要有一个根视图控制器。
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];
您可以使用以下方法添加视图
[[[UIApplication sharedApplication] keyWindow] addSubview:YOUR_VIEW];
您可以尝试以下代码…将子视图添加到窗口
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; aView.backgroundColor = [UIColor blackColor]; [[UIApplication sharedApplication].keyWindow addSubview:aView];
//删除
[aView removeFromSuperview];
希望它能帮助你……!
我想你错过了这个。 检查一次。
[self.window makeKeyAndVisible];
当您调用show方法时,UIAlertView会创建另一个UIWindow并将此窗口设置为关键窗口。 因此,如果要显示自己的警报视图,请创建一个UIWindow实例,然后将该自定义警报视图添加到此新创建的窗口中。