How to set iPhone UIView z index?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
UIKit does not have a CSS-style z-index property on UIView. The usual way to control which view appears on top is to reorder subviews within the same parent using methods such as bringSubviewToFront, sendSubviewToBack, or insertSubview. For more specialized cases, you can also adjust the underlying layer's zPosition.
Default Stacking Behavior
Within one parent view, later subviews are drawn above earlier ones. So if you do this:
then blueView appears above redView if they overlap.
That means the simplest "z-index" control is just managing the order in the parent view's subviews array.
The Usual UIKit Methods
To move a view above its siblings:
To move it behind its siblings:
To place it at a specific sibling index:
Or relative to another sibling:
For ordinary overlapping UI, these methods are the most readable and most maintainable answer.
Complete Example
After bringSubviewToFront(redView), the red view sits above the blue view.
When layer.zPosition Is Useful
Every UIView has a backing CALayer, and CALayer does have a zPosition property:
This can work, but it should not be your default tool for ordinary sibling ordering. A changed zPosition affects layer compositing and can make the hierarchy harder to reason about, especially if touch handling, transforms, shadows, or animations are involved.
Use zPosition when you specifically need layer-level depth behavior, not just "put this sibling on top".
Important Limitation: Same Parent Matters
Subview ordering methods compare siblings inside the same parent. If two views live in different branches of the view hierarchy, calling bringSubviewToFront on one parent will not change how that view stacks against views owned by another parent.
In those situations, you may need to:
- move the views under a common parent
- reorder higher-level container views
- use overlay container views intentionally
This is one reason complex iOS UIs sometimes feel confusing when developers think only in terms of a flat z-index.
Auto Layout Does Not Change the Rule
Auto Layout affects size and position, not sibling draw order. A view can be perfectly constrained and still sit behind another view simply because of the hierarchy order.
So if a view looks "hidden", do not debug only constraints. Check the stacking order too.
Common Pitfalls
The most common mistake is reaching for layer.zPosition when a simple bringSubviewToFront would have solved the problem more clearly.
Another issue is forgetting that ordering works among sibling views only. Developers also sometimes assume Auto Layout constraints determine depth. They do not. Finally, excessive zPosition usage can produce confusing rendering or hit-testing behavior if the view hierarchy is already complicated.
Summary
- '
UIViewdoes not expose a direct CSS-stylez-indexproperty.' - The normal UIKit solution is to reorder sibling views with
bringSubviewToFront,sendSubviewToBack, orinsertSubview. - Later-added siblings appear above earlier ones by default.
- '
layer.zPositionexists, but it is a more specialized tool.' - If overlapping views are in different branches, fix the hierarchy rather than only tweaking one view.

