What is the difference between addSubview
and insertSubView
methods when a view is added programmatically?
Asked
Active
Viewed 6.4k times
88

christopher.online
- 2,614
- 3
- 28
- 52

Ashwani K
- 7,880
- 19
- 63
- 102
4 Answers
47
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.

Nikolai Ruhe
- 81,520
- 17
- 180
- 200
-
Thanks, I wanted to is there specific difference in uses of these two functions – Ashwani K Oct 05 '09 at 10:30
-
1Aside from the specific difference I described in my answer, there is none. – Nikolai Ruhe Oct 05 '09 at 16:41
29
I don't think there is a difference. addSubview:
is simple a convenient method for
[view insertSubview:aView atIndex:[view.subviews count]]

sliver
- 1,650
- 2
- 14
- 23
-1
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];
}

Carlo
- 1,686
- 3
- 29
- 43

user2369870
- 9
- 2