[英]How to add View below other view in ViewController in iOS?
[英]How to add a view behind other view in iOS
我想在iOS中的標簽欄模擬下添加一個視圖,然后用它后面的動畫顯示它。 但是當我使用我的代碼時,必須來自我的標簽欄后面的視圖會重疊我的標簽欄一段時間。
這是我的代碼:
- (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];
}
}
這里的leftPanelView是必須位於標簽欄下的視圖。 移動視圖是另一個視圖,位於左側面板的右側(不要介意)
除了addSubview
之外,您還可以依賴其他方法來添加視圖:
– insertSubview:aboveSubview:
– insertSubview:belowSubview:
– insertSubview:atIndex:
您可以使用這些方法控制視圖的索引(實際上是z-index或圖層順序)。
[self.view sendSubviewToBack:leftPanelView];
在視圖層次結構中的另一個視圖下方 插入視圖:
view.insertSubview(subview1, belowSubview: subview2)
在視圖層次結構中的另一個視圖上方 插入視圖:
view.insertSubview(subview1, aboveSubview: subview2)
在指定的索引處 插入子視圖:
view.insertSubview(subview, at: index)
移動指定的子視圖,使其顯示在其兄弟節點后面 :
subview1.sendSubviewToBack(subview2)
移動指定的子視圖,使其顯示在其兄弟節點之上 :
subview1.bringSubviewToFront(subview2)
在指定的索引處 交換子視圖:
view.exchangeSubview(at: index1, withSubviewAt: index2)
哦,我自己找到了解決方案。 我在此之后添加了:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.view addSubview:leftPanelView];
這一行:
[self.view sendSubviewToBack:leftPanelView];
在Swift 5中
self.view.sendSubviewToBack(leftPanelView)
另一個好主意是給Subviews一個標簽 。
迅速
mainView.addSubView(topView)
mainView.addSubView(bottomView)
topView.tag = 2
mainView.insertSubview(bottomView, belowSubview: mainView.viewWithTag(2))
在視圖層次結構中的另一個視圖下方 插入視圖:
- (void)insertSubview:(UIView *)subview1
belowSubview:(UIView *)subview2;
在視圖層次結構中的另一個視圖上方 插入視圖:
- (void)insertSubview:(UIView *)subview1
aboveSubview:(UIView *)subview2;
在指定的索引處 插入子視圖:
- (void)insertSubview:(UIView *)view
atIndex:(NSInteger)index;
移動指定的子視圖,使其顯示在其兄弟節點后面 :
- (void)sendSubviewToBack:(UIView *)view;
移動指定的子視圖,使其顯示在其兄弟節點之上 :
- (void)bringSubviewToFront:(UIView *)view;
在指定的索引處 交換子視圖:
- (void)exchangeSubviewAtIndex:(NSInteger)index1
withSubviewAtIndex:(NSInteger)index2;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.