Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a simple case of insertSubview:belowSubview doesnt work

I went over many examples of this method and still cant figure out whats wrong with what I am doing. I am trying to add another view below my current view, then remove my current view once the user clicks on a button that is on the current screen. From some reason, the view disappears, but i dont see the view that supposed to be below it. I am just getting a white screen.

Here is the ViewDidLoad of my current (the one which is on the top - ViewControllerTop for the sake of the example) view:

- (void)viewDidLoad
{
[super viewDidLoad];
viewControllerBottom = [[ViewControllerBottom alloc] init];
[self.view insertSubview:viewControllerBottom.view belowSubview:self.view];

}

following this method, there is a method that triggers once the user clicks on the button:

- (IBAction)goToBottomView:(id)sender { 
[self.view.superview removeFromSuperview];

}

does anyone see anything wrong with this? Thanks for the help!

BTW - even though it's silly, I also tried to use insertSubview:aboveSubview but it doesnt work either.

like image 317
TommyG Avatar asked Jul 26 '11 23:07

TommyG


2 Answers

I think there's a fundamental mistake on your code. You are doing:

[self.view insertSubview:viewControllerBottom.view belowSubview:self.view]

so the view you pass in the belowSubview: parameter needs to be a subview of self.view, which self.view is not!

insertSubview:belowSubview: will work fine if you use it with subviews of the receiver object, as intended.

like image 198
pgb Avatar answered Sep 21 '22 07:09

pgb


The other fundamental mistake in the above code is that this new view does not have a frame defined. Instead of calling init which initializes the view to null rect, you should use a proper frame. Something like self.frame should work to cover the whole view.

And BTW, you cannot do both addSubview and then call insertSubView. Looking at what you want to achieve, you should insert this new view to the parent.

viewControllerBottom = [[ViewControllerBottom alloc] initWithFrame:self.frame];
[self.view.superView insertSubview:viewControllerBottom.view belowSubview:self.view];
like image 41
Deepak G M Avatar answered Sep 20 '22 07:09

Deepak G M