【问题标题】:Difference between addSubview and insertSubview in UIView classUIView 类中 addSubview 和 insertSubview 的区别
【发布时间】:2010-12-03 21:33:47
【问题描述】:

以编程方式添加视图时,addSubviewinsertSubView 方法有什么区别?

【问题讨论】:

    标签: iphone uiview uikit subview addsubview


    【解决方案1】:

    唯一的区别在于添加视图的位置:它是最前面的视图 (addSubview:),还是在第 5 个子视图之前 (insertSubview:atIndex:),或者它是否紧跟在另一个子视图之后 (@987654323 @)。

    【讨论】:

      【解决方案2】:

      使用insertSubView: 可以指定索引,该索引确定视图的z 顺序。索引较高的视图位于索引较低的视图之上。

      【讨论】:

      • 谢谢,我想问一下这两个功能在使用上有什么区别
      • 除了我在回答中描述的具体区别之外,没有任何区别。
      【解决方案3】:

      我认为没有区别。 addSubview: 是一种简单方便的方法

      [view insertSubview:aView atIndex:[view.subviews count]]
      

      【讨论】:

        【解决方案4】:

        1.addSubview 在数组中添加子视图,然后在View'slayer中添加

        - (void)addSubview:(UIView *)subview
        {
            [_subviews addObject:subview];
            [_layer addSublayer:subview.layer];
        }
        

        }

        2.虽然 insertSubview 将您的视图添加为子视图,然后调用 [_layer insertSublayer:subview.layer atIndex:index];

        - (void)insertSubview:(UIView *)subview atIndex:(NSInteger)index
        {
          [self addSubview:subview];
          [_layer insertSublayer:subview.layer atIndex:index];
        }
        

        【讨论】:

          猜你喜欢
          • 2015-08-06
          • 1970-01-01
          • 2011-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多