Difference between addSubview and insertSubview in

2019-01-10 10:11发布

What is the difference between addSubview and insertSubView methods when a view is added programmatically?

4条回答
成全新的幸福
2楼-- · 2019-01-10 10:52

I don't think there is a difference. addSubview: is simple a convenient method for

[view insertSubview:aView atIndex:[view.subviews count]]
查看更多
狗以群分
3楼-- · 2019-01-10 10:58

1.addSubview add subview in array then add in View'slayer

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

}

2.While insertSubview add your view as subview then call [_layer insertSublayer:subview.layer atIndex:index];

- (void)insertSubview:(UIView *)subview atIndex:(NSInteger)index
{
  [self addSubview:subview];
  [_layer insertSublayer:subview.layer atIndex:index];
}
查看更多
欢心
4楼-- · 2019-01-10 11:11

The only difference is in where the view is added: whether it is the frontmost view (addSubview:), or it is before the 5th subview, (insertSubview:atIndex:) or if it is immediately behind another subview (insertSubview:aboveSubview:).

查看更多
Animai°情兽
5楼-- · 2019-01-10 11:13

Using insertSubView: you can specify the index, which determines z-order of views. A view with a higher index lies above those with lower indices.

查看更多
登录 后发表回答