Safe area layout guides in xib files - iOS 10

23,375

Solution 1

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 superview.top and a 750 priority == safearea.top. 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

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: view.top == safe area.top
  • for earlier versions: view.top == superview.top + 20

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

override func viewDidLoad() {
    if #available(iOS 11.0, *) {
        view.removeConstraint(myConstraintSUPERVIEW)
    } else {
        view.removeConstraint(myConstraintSAFEAREA)
    }
}

Solution 3

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

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 SafeArea.top
    • 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;
        self.constraint.active = NO;
    }
    

Edit:

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) {
        constraintAttachedToSafeAreaTop.active = NO;
    }
}

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

Share:
23,375
Tiago Lira
Author by

Tiago Lira

iOS developer.

Updated on July 05, 2022

Comments

  • Tiago Lira
    Tiago Lira almost 2 years

    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

  • Tiago Lira
    Tiago Lira almost 7 years
    My specific problem is only on XIB files, and seems to happen for both pushed and presented controllers. Your workaround with multiple constraints sounds like a good fix for most situations. Thanks! I'm still hoping they will solve these issues before the iPhoneX arrives.
  • alex.bour
    alex.bour almost 7 years
    Very thank for the priority suggestion ! Now works like a charm on iOS 9 and iOS 11 under iPhone X.
  • Stanislav Dvoychenko
    Stanislav Dvoychenko almost 7 years
    May I comment ask on this "If you always show a status bar, that should fix things."? I can't see the way to show status bar on iPhone X simulator in landscape. Then the described workaround of "1000 priority >= 20.0 to superview.top and a 750 priority == safearea.top" would have a 20px gap on top for the iPhone X in landscape. My solution was to remove the constraint of "1000 priority >= 20.0" in the viewDidLoad if iOS < 11. Am I missing the way to force status bar in iPhone X landscape?
  • clarus
    clarus almost 7 years
    That’s a good point. I was only dealing with portrait for the app i was working with. For iOS 11, theres no reason to have that constraint anyway. That sounds like a good solution, or programmatically adding a top layout guide constraint for < ios 11 and not always having that >= 20 one.
  • Stanislav Dvoychenko
    Stanislav Dvoychenko over 6 years
    @clarus, Good point as well. Probably depends on how explicit/implicit one would need this handling to be. In the end, I decided to keep the xib with Safe area, "1000 priority >= 20.0" in the xib and removing the constraint in (if (@available(iOS 11.0, *))). I have only 2 of such xibs, used for a lot of controllers (VC inheritance is why I can't move them to storyboards simply). In case on these 2, I probably will prefer to keep everything very explicit in both xibs and code. Should I have tens of these, I'd probably go with your suggested order, future maintenance wise.
  • AnBisw
    AnBisw over 6 years
    The priority setup you mentioned is the best thing I've seen all week. It was driving me nuts. Thank you.
  • C0D3
    C0D3 over 6 years
    I think having seperate storyboards is not a good solution. I'd rather write more code to avoid this
  • Jesse Naugher
    Jesse Naugher about 6 years
    This is a much more maintainable/cleaner approach than the accepted answer, imo. Note this does depend on having a status bar always
  • Jack
    Jack about 6 years
    Instead of giving double constraint Apple should provide backward compatibility for ios<11 @clarus or it might include in wwdc 2018 ?
  • jowie
    jowie almost 6 years
    Great solution, thanks! One question though, when adding the superview.top constraint, should it be relative to margins or not?
  • Tumata
    Tumata almost 6 years
    I don't think this is a good idea because since viewDidLayoutSubviews is called on any bounds change, this will create a new constraint and activate it on every single bounds change.
  • Jonny
    Jonny almost 6 years
    Yes, you need to make sure that if it exists you don't create another one.
  • Brian Sachetta
    Brian Sachetta almost 6 years
    I agree - have been looking around for answers and this seems way cleaner than having separate XIB files for iOS 10 and iOS 11.
  • Maulik shah
    Maulik shah almost 6 years
    can you help me for objective c
  • OliverD
    OliverD almost 5 years
    Unfortunately this only works if the navigation bar is visible. If the nav bar is hidden, the view will still extend under the status bar with edgesForExtendedLayout = []