0

I use keyWindow to add a subView, it add success but did not shows in the front of the screen hierarchy.

The deep gray view is my added view:

enter image description here

My code is:

@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];

}

I have tried use [keywindow bringSubviewToFront:pop_v] to bring to front.But do not work.

And if I use [self.view addSubview:pop_v], it shows in the front.

s-n-2
  • 405
  • 1
  • 6
  • 24

3 Answers3

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.

Community
  • 1
  • 1
aircraft
  • 25,146
  • 28
  • 91
  • 166
0

You can solve this problem using appdelegate's Windows :

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:pop_v]
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

Use this:

@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];

}
Sargis
  • 1,196
  • 1
  • 18
  • 31