Safe area layout guides in xib files - iOS 10

IosIos10Xcode9Iphone X

Ios Problem Overview

I started adapting my app for iPhone X and found an issue in Interface Builder. The safe area layout guides are supposed to be backwards compatible, according to official Apple videos. I found that it works just fine in storyboards.

But in my XIB files, the safe area layout guides are not respected in iOS 10.

They work fine for the new OS version, but the iOS 10 devices seem to simply assume the safe area distance as zero (ignoring the status bar size).

Am I missing any required configuration? Is it an Xcode bug, and if so, any known workarounds?

Here is a screenshot of the issue in a test project (left iOS 10, right iOS 11):

safe area alignment on top of the screen

Ios Solutions

Solution 1 - Ios

There are some issues with safe area layout and backwards compatibility. See my comment over here.

You might be able to work around the issues with additional constraints like a 1000 priority >= 20.0 to and a 750 priority == If you always show a status bar, that should fix things.

A better approach may be to have separate storyboards/xibs for pre-iOS 11 and iOS-11 and up, especially if you run into more issues than this. The reason that's preferable is because pre-iOS 11 you should layout constraints to the top/bottom layout guides, but for iOS 11 you should lay them out to safe areas. Layout guides are gone. Laying out to layout guides for pre-iOS 11 is stylistically better than just offsetting by a min of 20 pixels, even though the results will be the same IFF you always show a status bar.

If you take this approach, you'll need to set each file to the correct deployment target that it will be used on (iOS 11, or something earlier) so that Xcode doesn't give you warnings and allows you to use layout guides or safe areas, depending. In your code, check for iOS 11 at runtime and then load the appropriate storyboard/xibs.

The downside of this approach is maintenance, (you'll have two sets of your view controllers to maintain and keep in sync), but once your app only supports iOS 11+ or Apple fixes the backward compatibility layout guide constraint generation, you can get rid of the pre-iOS 11 versions.

By the way, how are you displaying the controller that you're seeing this with? Is it just the root view controller or did you present it, or..? The issue I noticed has to do with pushing view controllers, so you may be hitting a different case.

Solution 2 - Ios

Currently, backward compatibility doesn't work well.

My solution is to create 2 constraints in interface builder and remove one depending on the ios version you are using:

  • for ios 11: == safe
  • for earlier versions: == + 20

Add them both as outlets as myConstraintSAFEAREA and myConstraintSUPERVIEW respectively. Then:

override func viewDidLoad() {
    if #available(iOS 11.0, *) {
    } else {

Solution 3 - Ios

For me, a simple fix for getting it to work on both versions was

    if #available(iOS 11, *) {}
    else {
        self.edgesForExtendedLayout = []

From the documentation: "In iOS 10 and earlier, use this property to report which edges of your view controller extend underneath navigation bars or other system-provided views. ". So setting them to an empty array makes sure the view controller does not extend underneath nav bars.

Docu is available here

Solution 4 - Ios

I have combined some of the answers from this page into this, which works like a charm (only for top layout guide, as requested in the question):

  1. Make sure to use safe area in your storyboard or xib file

  2. Constraint your views to the safe areas

  3. For each view which has a constraint attached to the

    • Create an IBOutlet for the view
    • Create an IBOutler for the constraint
  4. Inside the ViewController on viewDidLoad:

    if (@available(iOS 11.0, *)) {} else { // For each view and constraint do: [self.view.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor].active = YES; = NO; }


Here is the improved version I ended up using in our codebase. Simply copy/paste the code below and connect each view and constraints to their IBOutletCollection.

@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *constraintsAttachedToSafeAreaTop;
@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *viewsAttachedToSafeAreaTop;

if (@available(iOS 11.0, *)) {}
else {
    for (UIView *viewAttachedToSafeAreaTop in self.viewsAttachedToSafeAreaTop) {
        [viewAttachedToSafeAreaTop.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor].active = YES;
    for (NSLayoutConstraint *constraintAttachedToSafeAreaTop in self.constraintsAttachedToSafeAreaTop) { = NO;

> The count of each IBOutletCollection should be equal. e.g. for each view > there should be its associated constraint

Solution 5 - Ios

I ended up deleting the constraint to safe area which I had in my xib file. Instead I made an outlet to the UIView in question, and from code I hooked it up like this, in viewDidLayoutSubviews.

let constraint = alert.viewContents.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor, constant: 0)
constraint.priority = 998
constraint.isActive = true

This ties a small "alert" to top of screen but makes sure that the contents view within the alert is always below the top safe area(iOS11ish)/topLayoutGuide(iOS10ish)

Simple and a one-off solution. If something breaks, I'll be back .

Solution 6 - Ios

This also works:

override func viewDidLoad() {
    if #available(iOS 11.0, *) {}
    else {
        view.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height - 80).isActive = true
        view.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - 20).isActive = true

Solution 7 - Ios

I added a NSLayoutConstraint subclass to fix this problem (IBAdjustableConstraint), with a @IBInspectable variable, looks like this.

class IBAdjustableConstraint: NSLayoutConstraint {

    @IBInspectable var safeAreaAdjustedConstant: CGFloat = 0 {
        didSet {
            if OS.TenOrBelow {
                constant += safeAreaAdjustedConstantLegacy

And OS.TenOrBelow

struct OS {
    static let TenOrBelow ="10.9", options: NSString.CompareOptions.numeric) == ComparisonResult.orderedAscending

Just set that as the subclass of your constraint in IB and you will be able to make < iOS11 specific changes. Hope this helps someone.

Solution 8 - Ios

I used this one, add the top safe area layout and connect with outlet

@IBOutlet weak var topConstraint : NSLayoutConstraint!

override func viewDidLoad() {
    if !DeviceType.IS_IPHONE_X {
        if #available(iOS 11, *)  {
            topConstraint.constant = 20

Solution 9 - Ios

Found the simpliest solution - just disable safe area and use topLayoutGuide and bottomLayoutGuide + add fixes for iPhone X. Maybe it is not beautiful solution but requires as less efforts as possible


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
QuestionTiago LiraView Question on Stackoverflow
Solution 1 - IosclarusView Answer on Stackoverflow
Solution 2 - IosMartin MasseraView Answer on Stackoverflow
Solution 3 - IosFalco WinklerView Answer on Stackoverflow
Solution 4 - IosTumataView Answer on Stackoverflow
Solution 5 - IosJonnyView Answer on Stackoverflow
Solution 6 - IosMenan VadivelView Answer on Stackoverflow
Solution 7 - IosAlex BrownView Answer on Stackoverflow
Solution 8 - IoslittleView Answer on Stackoverflow
Solution 9 - IosVyachaslav GerchicovView Answer on Stackoverflow