I have two views (UIView) set-up: parent and child view. The child view is displayed on top of the parent view, as desired. However I need to add subviews now that I would like to be displayed below the child view, but above the parent view. What is an easy way of keeping the first child view on top while adding new subviews? I would like to keep the newly added subviews display order match insertion order.
Asked
Active
Viewed 7,088 times
2 Answers
10
Use any of the following UIView
methods to control depth.
– bringSubviewToFront:
– sendSubviewToBack:
– insertSubview:atIndex:
– insertSubview:aboveSubview:
– insertSubview:belowSubview:
This is taken from the View Programming Guide docs in the View Hierarchies and Subview Management section,
Each superview stores its subviews in an ordered array and the order in that array also affects the visibility of each subview. If two sibling subviews overlap each other, the one that was added last (or was moved to the end of the subview array) appears on top of the other.

Anurag
- 140,337
- 36
- 221
- 257
1
I'd suggest creating wrapper method for addSubview
. Something like this:
- (void)insertSubviewBetweenParentAndChild:(UIView *)newChild {
[self insertSubview:newChild aboveSubview:parentView];
}

Eimantas
- 48,927
- 17
- 132
- 168