Difference between addSubview and insertSubview in UIView class

社会主义新天地 提交于 2019-12-17 10:20:13

问题


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


回答1:


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:).




回答2:


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.




回答3:


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

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



回答4:


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];
}


来源:https://stackoverflow.com/questions/1519141/difference-between-addsubview-and-insertsubview-in-uiview-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!