Difference between addSubview and insertSubview in UIView class

62,470

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

Solution 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.

Solution 3

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

[view insertSubview:aView atIndex:[view.subviews count]]
Share:
62,470
Ashwani K
Author by

Ashwani K

Full-stack developer with experience in developing Spring, AWS, Android, java, nodejs, and many more applications.

Updated on December 09, 2020

Comments

  • Ashwani K
    Ashwani K over 3 years

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

  • Ashwani K
    Ashwani K over 14 years
    Thanks, I wanted to is there specific difference in uses of these two functions
  • Nikolai Ruhe
    Nikolai Ruhe over 14 years
    Aside from the specific difference I described in my answer, there is none.