ios 如何在 UIWindow 中添加视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29160540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add view in UIWindow?
提问by Prajeet Shrestha
I wanted to add a view in UIWindow
with following code:
我想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];
This code didn't work. I wanted to clone property of UIAlertView. It will pop over top of everything when we call
[alertViewInstance show];
method.
此代码不起作用。我想克隆 UIAlertView 的属性。当我们调用[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];
回答by Vishal16
If are you using UINavigationController
Use:
如果您使用UINavigationController
使用:
[self.navigationController.view.window addSubview:aView];
If are you using UITabBarController
Use:
如果您使用UITabBarController
使用:
[self.tabBarController.view.window addSubview:aView];
In AppDelegate
you can directly assign a view to window. In appDelegate
didFinishLaunchingWithOptions
method Use:
在AppDelegate
您可以直接分配一个视图窗口。在appDelegate
didFinishLaunchingWithOptions
方法中使用:
[self.window addSubview:aView];
Hope it helps...
希望能帮助到你...
回答by TienLe
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];
}
回答by Steven Gardner
Try this code:
试试这个代码:
window = [[UIApplication sharedApplication].windows lastObject];
Replace the following code:
替换以下代码:
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
回答by Shekhu
You can add view using the following
您可以使用以下方法添加视图
[[[UIApplication sharedApplication] keyWindow] addSubview:YOUR_VIEW];
回答by Nikos M.
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];
回答by Vidhyanand
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..!
希望能帮到你..!
回答by Mahesh Agrawal
i think you are missing this. check once.
我想你错过了这个。检查一次。
[self.window makeKeyAndVisible];
回答by Nitheesh George
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.
UIAlertView 创建另一个 UIWindow 并在调用 show 方法时将此窗口设置为关键窗口。因此,如果您想显示您自己的警报视图,请创建一个 UIWindow 实例来显示它并将您的自定义警报视图添加到这个新创建的窗口中。