Difference between addSubview and insertSubview in UIView class

IphoneUiviewUikitSubviewAddsubview

Iphone Problem Overview


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

Iphone Solutions


Solution 1 - Iphone

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

Solution 2 - Iphone

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.

Solution 3 - Iphone

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

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

Solution 4 - Iphone

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAshwani KView Question on Stackoverflow
Solution 1 - IphonemahboudzView Answer on Stackoverflow
Solution 2 - IphoneNikolai RuheView Answer on Stackoverflow
Solution 3 - IphonesliverView Answer on Stackoverflow
Solution 4 - Iphoneuser2369870View Answer on Stackoverflow