0

我使用keyWindow添加了一个子视图,它添加成功但没有显示在屏幕层次结构的前面。

深灰色视图是我添加的视图:

在此处输入图像描述

我的代码是:

@interface ViewController ()
{
    LMLUpspringView *pop_v;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    pop_v = [[LMLUpspringView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

    UIWindow *keywindow = [[[UIApplication sharedApplication] delegate] window];

    [keywindow addSubview:pop_v];

    //[keywindow bringSubviewToFront:pop_v];
    //[self.view addSubview:pop_v];

}

我曾尝试使用[keywindow bringSubviewToFront:pop_v]带到前面。但不工作。

如果我使用[self.view addSubview:pop_v],它会显示在前面。

4

3 回答 3

2

You run into this issue, is caused by you do not know the difference between the viewDidLoad method and the viewDidAppear: method.

viewWillAppear:

Called before the view is added to the windows’ view hierarchy

viewDidAppear:

Called after the view is added to the view hierarchy

The controller's view add to the window's view hierarchy is betweenviewWillAppear: and viewDidAppear:, the viewDidLoad is in front of viewWillAppear:, so you can not do that. you should in viewDidAppear: add your view.

于 2017-04-25T08:47:31.013 回答
0

You can solve this problem using appdelegate's Windows :

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:pop_v]
于 2017-04-25T08:46:49.207 回答
0

用这个:

@interface ViewController ()
{
    LMLUpspringView *pop_v;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    pop_v = [[LMLUpspringView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];

    [keywindow addSubview:pop_v];

    //[keywindow bringSubviewToFront:pop_v];
    //[self.view addSubview:pop_v];

}
于 2017-04-25T10:21:44.920 回答