iOS view doesn't display any of it's subviews when added as a subview of NavigationController's view

1.1k views Asked by At

I have a NavigationController and I want to make a View that animates itself from the top of the screen and sits above navigationBar. I'm initialising viewcontroller from a xib and it works perfectly when I add it as [self.view addSubview:myView]; , but it sits below navigation bar. When I try to add it as [self.navigationController.view addSubview:myView]; it places it on top of navigationBar as intended but view doesn't display any of it's subviews placed in xib. I can't see why this is happening, have been trying to solve this for hours.

UPDATE My custom view from a xib is paired with custom ViewController class and I added this code in it :

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

NSLog(@"!!! %i", [self.view.subviews count]);

}

if I add myView to self.view it prints 1 (there's actually 1 subview), but when I add it as self.navigationController.view -viewDidAppear is not even called. But it does appear on the screen with no subviews. Now I'm even more confused.

3

There are 3 answers

0
NKorotkov On BEST ANSWER

Turns out I'm an idiot. It's a really simple scope problem. I was instantiating a local variable of my VC inside the method, and of course it was released at the end. As soon as I assigned my VC to a global variable all started to work like a charm. Easy as that.

5
mityaika07 On

I think you need use this:

[self.navigationController.navigationBar addSubview:myView];

1
Krishna On

Why don't you just remove the navigation controller if you don't need it? Another way is to hide the navigation bar so that view occupies complete space and then show the navigation bar again when needed. I could help more if you provide some code with the views.