Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a view behind other view in iOS

I want to add a view under my tab bar analog in iOS and then show it with animation coming from behind it. But when I use my code, the view that must come from behind my tab bar overlaps my tab bar for a while.

tab bar and a view that shows when tab bar button pressed

This is my code:

- (void)handlePressedButton { if (pressedButton.selected) {     [UIView beginAnimations:nil context:nil];     [UIView setAnimationDuration:0.5];     CGAffineTransform transform1 = CGAffineTransformMakeTranslation(-196.0, 0.0);     CGAffineTransform transform2 = CGAffineTransformMakeTranslation(0.0, 0.0);     [leftPanelView setTransform:transform1];     [movedView setTransform:transform2];     [UIView commitAnimations];     pressedButton.selected = NO; } else {     pressedButton.selected = YES;     if (leftPanelView) {         [leftPanelView removeFromSuperview];         leftPanelView = nil;     }     CGRect viewFrame = CGRectMake(-196, 0, 236, 748);     leftPanelView = [[UIView alloc] initWithFrame:viewFrame];     leftPanelView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"left-nav-content.png"]];      // Code to populate leftPanelView according to what button is pressed.     [self populateLeftPanelView];      [UIView beginAnimations:nil context:nil];     [UIView setAnimationDuration:0.5];     [self.view addSubview:leftPanelView];     CGAffineTransform transform1 = CGAffineTransformMakeTranslation(236.0, 0.0);     CGAffineTransform transform2 = CGAffineTransformMakeTranslation(112.0, 0.0);     [leftPanelView setTransform:transform1];     [movedView setTransform:transform2];     [UIView commitAnimations]; } 

}

Here the leftPanelView is the view that must come under a tab bar. Moved view is another view, that is at the right of left panel (don't mind it)

like image 810
Denis Kutlubaev Avatar asked Mar 20 '12 15:03

Denis Kutlubaev


2 Answers

Besides addSubview, there are other methods you can rely on to add your view:

– insertSubview:aboveSubview: – insertSubview:belowSubview: – insertSubview:atIndex: 

You can control the index (actually z-index, or layer order) of the view using these methods.

like image 57
He Shiming Avatar answered Oct 07 '22 02:10

He Shiming


try sendSubviewToBack

[self.view sendSubviewToBack:leftPanelView]; 
like image 39
ader Avatar answered Oct 07 '22 01:10

ader