I went over many examples of this method and still can开发者_如何转开发t 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.
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.
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];
精彩评论