如何在 iOS 中的其他视图后面添加一个视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9790040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a view behind other view in iOS
提问by Denis Kutlubaev
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.
我想在 iOS 中我的标签栏模拟下添加一个视图,然后用来自它后面的动画显示它。但是当我使用我的代码时,必须来自我的标签栏后面的视图与我的标签栏重叠了一段时间。
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)
这里的 leftPanelView 是必须位于标签栏下的视图。移动视图是另一个视图,位于左侧面板的右侧(不要介意)
回答by He Shiming
Besides addSubview
, there are other methods you can rely on to add your view:
此外addSubview
,您还可以依赖其他方法来添加视图:
– insertSubview:aboveSubview:
– insertSubview:belowSubview:
– insertSubview:atIndex:
You can control the index (actually z-index, or layer order) of the view using these methods.
您可以使用这些方法控制视图的索引(实际上是 z-index 或图层顺序)。
回答by ader
回答by xmasalov
Swift 5
斯威夫特 5
Inserta view belowanother view in the view hierarchy:
在视图层次结构中的另一个视图下方插入一个视图:
view.insertSubview(subview1, belowSubview: subview2)
Inserta view aboveanother view in the view hierarchy:
在视图层次结构中的另一个视图上方插入一个视图:
view.insertSubview(subview1, aboveSubview: subview2)
Inserta subview at the specified index:
在指定的索引处插入一个子视图:
view.insertSubview(subview, at: index)
Movethe specified subview so that it appears behindits siblings:
移动指定的子视图,使其出现在其兄弟视图的后面:
subview1.sendSubviewToBack(subview2)
Movethe specified subview so that it appears on topof its siblings:
移动指定的子视图,使其出现在其兄弟视图之上:
subview1.bringSubviewToFront(subview2)
Exchangethe subviews at the specified indices:
交换指定索引处的子视图:
view.exchangeSubview(at: index1, withSubviewAt: index2)
回答by Denis Kutlubaev
Oh, I found a solution myself. I added after this:
哦,我自己找到了解决方案。我在这之后补充道:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.view addSubview:leftPanelView];
this line:
这一行:
[self.view sendSubviewToBack:leftPanelView];
回答by Vinoth Vino
In Swift 5
在斯威夫特 5
self.view.sendSubviewToBack(leftPanelView)
回答by myra
Another nice idea is to give the Subviews a tag.
另一个好主意是给子视图一个标签。
Swift
迅速
mainView.addSubView(topView)
mainView.addSubView(bottomView)
topView.tag = 2
mainView.insertSubview(bottomView, belowSubview: mainView.viewWithTag(2))
回答by xmasalov
Objective-C
目标-C
Inserta view belowanother view in the view hierarchy:
在视图层次结构中的另一个视图下方插入一个视图:
- (void)insertSubview:(UIView *)subview1
belowSubview:(UIView *)subview2;
Inserta view aboveanother view in the view hierarchy:
在视图层次结构中的另一个视图上方插入一个视图:
- (void)insertSubview:(UIView *)subview1
aboveSubview:(UIView *)subview2;
Inserta subview at the specified index:
在指定的索引处插入一个子视图:
- (void)insertSubview:(UIView *)view
atIndex:(NSInteger)index;
Movethe specified subview so that it appears behindits siblings:
移动指定的子视图,使其出现在其兄弟视图的后面:
- (void)sendSubviewToBack:(UIView *)view;
Movethe specified subview so that it appears on topof its siblings:
移动指定的子视图,使其出现在其兄弟视图之上:
- (void)bringSubviewToFront:(UIView *)view;
Exchangethe subviews at the specified indices:
交换指定索引处的子视图:
- (void)exchangeSubviewAtIndex:(NSInteger)index1
withSubviewAtIndex:(NSInteger)index2;