Difference between addSubview and insertSubview in UIView class
Asked Answered
M

4

88

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

Massey answered 5/10, 2009 at 9:54 Comment(0)
M
105

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

Muddle answered 5/10, 2009 at 11:18 Comment(0)
G
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.

Gayl answered 5/10, 2009 at 10:10 Comment(2)
Thanks, I wanted to is there specific difference in uses of these two functionsMassey
Aside from the specific difference I described in my answer, there is none.Gayl
C
29

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

[view insertSubview:aView atIndex:[view.subviews count]]
Crimson answered 5/10, 2009 at 11:10 Comment(0)
F
-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];
}
Fabrication answered 23/1, 2016 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.