Iphone 如何在iOS中的其他视图后面添加视图

Iphone 如何在iOS中的其他视图后面添加视图,iphone,ios,view,core-animation,Iphone,Ios,View,Core Animation,我想在iOS中的选项卡栏下添加一个视图,然后用后面的动画显示它。但当我使用代码时,必须从选项卡栏后面出现的视图会与选项卡栏重叠一段时间 这是我的代码: - (void)handlePressedButton { if (pressedButton.selected) { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; CGAffineTransform transf

我想在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是必须位于选项卡栏下的视图。移动视图是另一个视图,位于左侧面板的右侧(不介意)

哦,我自己找到了解决方案。我在这之后补充说:

[UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [self.view addSubview:leftPanelView];
这一行:

[self.view sendSubviewToBack:leftPanelView];
试一试


除了添加子视图,您还可以使用其他方法添加视图:

– insertSubview:aboveSubview:
– insertSubview:belowSubview:
– insertSubview:atIndex:
您可以使用这些方法控制视图的索引(实际上是z索引或图层顺序)。

Swift 5 在视图层次结构中的另一个视图下方插入一个视图:

view.insertSubview(subview1, belowSubview: subview2)
view.insertSubview(subview1, aboveSubview: subview2)
- (void)insertSubview:(UIView *)subview1 
         belowSubview:(UIView *)subview2;
- (void)insertSubview:(UIView *)subview1 
         aboveSubview:(UIView *)subview2;
在视图层次结构中的另一个视图上方插入一个视图:

view.insertSubview(subview1, belowSubview: subview2)
view.insertSubview(subview1, aboveSubview: subview2)
- (void)insertSubview:(UIView *)subview1 
         belowSubview:(UIView *)subview2;
- (void)insertSubview:(UIView *)subview1 
         aboveSubview:(UIView *)subview2;
在指定索引处插入子视图

view.insertSubview(subview, at: index)
view.exchangeSubview(at: index1, withSubviewAt: index2)
- (void)insertSubview:(UIView *)view 
              atIndex:(NSInteger)index;
- (void)exchangeSubviewAtIndex:(NSInteger)index1 
            withSubviewAtIndex:(NSInteger)index2;

移动指定的子视图,使其显示在其同级视图的后面:

subview1.sendSubviewToBack(subview2)
subview1.bringSubviewToFront(subview2)
- (void)sendSubviewToBack:(UIView *)view;
- (void)bringSubviewToFront:(UIView *)view;
移动指定的子视图,使其显示在同级视图的顶部

subview1.sendSubviewToBack(subview2)
subview1.bringSubviewToFront(subview2)
- (void)sendSubviewToBack:(UIView *)view;
- (void)bringSubviewToFront:(UIView *)view;

交换指定索引处的子视图

view.insertSubview(subview, at: index)
view.exchangeSubview(at: index1, withSubviewAt: index2)
- (void)insertSubview:(UIView *)view 
              atIndex:(NSInteger)index;
- (void)exchangeSubviewAtIndex:(NSInteger)index1 
            withSubviewAtIndex:(NSInteger)index2;

Swift 5中

self.view.sendSubviewToBack(leftPanelView)

另一个好主意是给子视图添加一个标记

Swift

mainView.addSubView(topView)
mainView.addSubView(bottomView)

topView.tag = 2

mainView.insertSubview(bottomView, belowSubview: mainView.viewWithTag(2))
目标-C 在视图层次结构中的另一个视图下方插入一个视图:

view.insertSubview(subview1, belowSubview: subview2)
view.insertSubview(subview1, aboveSubview: subview2)
- (void)insertSubview:(UIView *)subview1 
         belowSubview:(UIView *)subview2;
- (void)insertSubview:(UIView *)subview1 
         aboveSubview:(UIView *)subview2;
在视图层次结构中的另一个视图上方插入一个视图:

view.insertSubview(subview1, belowSubview: subview2)
view.insertSubview(subview1, aboveSubview: subview2)
- (void)insertSubview:(UIView *)subview1 
         belowSubview:(UIView *)subview2;
- (void)insertSubview:(UIView *)subview1 
         aboveSubview:(UIView *)subview2;
在指定索引处插入子视图

view.insertSubview(subview, at: index)
view.exchangeSubview(at: index1, withSubviewAt: index2)
- (void)insertSubview:(UIView *)view 
              atIndex:(NSInteger)index;
- (void)exchangeSubviewAtIndex:(NSInteger)index1 
            withSubviewAtIndex:(NSInteger)index2;

移动指定的子视图,使其显示在其同级视图的后面:

subview1.sendSubviewToBack(subview2)
subview1.bringSubviewToFront(subview2)
- (void)sendSubviewToBack:(UIView *)view;
- (void)bringSubviewToFront:(UIView *)view;
移动指定的子视图,使其显示在同级视图的顶部

subview1.sendSubviewToBack(subview2)
subview1.bringSubviewToFront(subview2)
- (void)sendSubviewToBack:(UIView *)view;
- (void)bringSubviewToFront:(UIView *)view;

交换指定索引处的子视图

view.insertSubview(subview, at: index)
view.exchangeSubview(at: index1, withSubviewAt: index2)
- (void)insertSubview:(UIView *)view 
              atIndex:(NSInteger)index;
- (void)exchangeSubviewAtIndex:(NSInteger)index1 
            withSubviewAtIndex:(NSInteger)index2;

我接受你的回答,因为你已经在2分钟后发送了它,我张贴。我认为这是公平的。这并不能回答所提出的问题。所有这些操作都是将视图移动到子视图链的后面。何世明给出的以下答案是正确的。请检查是否有效:[self.navigationController.view insertSubview:belowSubview:self.navigationController.navigationBar];这应该是正确的答案。上面的答案不能回答所问的问题。