【问题标题】:How to add a view behind other view in iOS如何在iOS中的其他视图后面添加视图
【发布时间】:2012-04-05 02:40:12
【问题描述】:

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

【问题讨论】:

  • 检查这是否有效:[self.navigationController.view insertSubview: belowSubview:self.navigationController.navigationBar];

标签: iphone ios view core-animation


【解决方案1】:

除了addSubview,您还可以依靠其他方法来添加视图:

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

您可以使用这些方法控制视图的索引(实际上是 z-index,或层顺序)。

【讨论】:

  • 这应该是正确的答案。上面的答案没有回答所问的问题。
【解决方案2】:

试试sendSubviewToBack

[self.view sendSubviewToBack:leftPanelView];

【讨论】:

  • 我接受你的回答,因为你在 2 分钟后发送了它,我发布了。我认为这是公平的。
  • 这不能回答所提出的问题。所有这一切都是将视图移动到子视图链的后面。下面何世铭的回答是正确的。
【解决方案3】:

斯威夫特 5

插入视图视图层次结构中的另一个视图:

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)

【讨论】:

    【解决方案4】:

    哦,我自己找到了解决方案。我在此之后添加:

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

    这一行:

    [self.view sendSubviewToBack:leftPanelView];
    

    【讨论】:

      【解决方案5】:

      Swift 5

      self.view.sendSubviewToBack(leftPanelView)
      

      【讨论】:

        【解决方案6】:

        另一个好主意是给子视图一个标签

        斯威夫特

        mainView.addSubView(topView)
        mainView.addSubView(bottomView)
        
        topView.tag = 2
        
        mainView.insertSubview(bottomView, belowSubview: mainView.viewWithTag(2))
        

        【讨论】:

          【解决方案7】:

          目标-C

          插入视图视图层次结构中的另一个视图:

          - (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;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多