Safe Area of Xcode 9
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Xcode 9 introduced safe areas to replace the older top and bottom layout guides with one unified layout concept. A safe area marks the region where your interface can appear without being obscured by bars, rounded corners, the home indicator, or device cutouts.
This became especially important once iPhone screens stopped being simple rectangles. If your layout is pinned to the superview edges instead of the safe area, controls can end up partly hidden or awkwardly positioned on modern devices.
What a Safe Area Represents
The safe area is exposed in code through safeAreaLayoutGuide. It is not a fixed inset value that always stays the same. Instead, it adapts to the current container and environment.
For example, the safe area can change because of:
- a navigation bar or tab bar,
- device rotation,
- a modal presentation style,
- the home indicator or sensor housing,
- a parent view controller embedding the child view.
That is why safe areas are a layout rule, not just a hard-coded margin.
Using Safe Areas in Interface Builder
In Interface Builder, the normal workflow is to pin views to the safe area instead of to the root view's outer edges. For a label at the top of the screen, for example, you would create constraints from the label to the safe area's top, leading, and trailing anchors.
This gives Auto Layout enough information to move the label downward on devices where the physical top edge is not actually safe for content.
Safe areas also make older storyboard constraints easier to migrate because Xcode 9 can update many constraints automatically when you enable safe-area-based layout.
Using safeAreaLayoutGuide in Code
Here is a small programmatic example:
The important part is that the constraints are attached to view.safeAreaLayoutGuide, not to view.topAnchor and view.leadingAnchor directly.
Safe Area Versus Layout Margins
Safe areas and layout margins are related but different. The safe area protects content from being obscured by system UI and physical screen features. Layout margins are an additional design choice that create readable padding inside an already safe area.
A common pattern is:
- anchor a main container to the safe area,
- then use layout margins inside that container for visual spacing.
If you try to use margins to solve a safe-area problem, the UI can still end up under the notch or home indicator. The safe area has to come first.
Supporting Older Code
When maintaining code written before Xcode 9, you may still see topLayoutGuide and bottomLayoutGuide. Those were the earlier APIs for avoiding bars. Safe areas generalize that idea and work more consistently across new device shapes.
When modernizing old code, the safest approach is usually to replace those older guides with safeAreaLayoutGuide and then retest the screens on multiple simulators.
Common Pitfalls
- Constraining controls to the raw view edges instead of the safe area.
- Assuming the safe area is only about the notch. Bars, presentations, and container controllers affect it too.
- Confusing layout margins with safe-area protection.
- Hard-coding top padding values instead of letting Auto Layout respond to the safe area dynamically.
- Migrating old storyboards but not retesting on current device sizes and orientations.
Summary
- The safe area is the visible, unobstructed region where app content should normally live.
- Xcode 9 introduced
safeAreaLayoutGuideas the replacement for older top and bottom layout guides. - Pin controls to the safe area in Interface Builder and in code.
- Use layout margins for interior spacing after safe-area constraints are established.
- Safe-area-based layouts are essential for modern iOS devices and container-based interfaces.

