Difference between addSubview and insertSubview in UIView class

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

thank

Ashwani

+72
iphone uiview subview addsubview
05 Oct '09 at 9:54
source share
4 answers

The only difference is where the view is added: whether it is the front-most view ( addSubview: , or it is in front of the 5th subzone ( insertSubview:atIndex: , or if it is immediately behind another view ( insertSubview:aboveSubview: .

+92
Oct 05 '09 at 11:18
source share

Using insertSubView: you can specify an index that defines the z-order of the views. A view with a higher index is higher than those with lower indices.

+40
05 Oct '09 at 10:10
source share

I do not think there is a difference. addSubview: is a simple convenient method for

 [view insertSubview:aView atIndex:[view.subviews count]] 
+29
Oct 05 '09 at 11:10
source share

1.addSubview add subview to array and then add to View'slayer

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

}

2. If insertSubview adds your view as a subzone, then call [_layer insertSublayer:subview.layer atIndex:index];

 - (void)insertSubview:(UIView *)subview atIndex:(NSInteger)index { [self addSubview:subview]; [_layer insertSublayer:subview.layer atIndex:index]; } 
0
Jan 23 '16 at 10:45
source share



All Articles