Latest 2.0.2
Homepage https://github.com/truffls/compatible-layout-anchors-ios
License MIT
Platforms ios 9.0, requires ARC

alt text

Swift Version
Platform Compatibility
Pod version

An easy way to support iOS 11 safe area anchors and iOS <=10 top/bottom layout guides at the same time. CompatibleLayoutAnchors takes away the boiler plate code to check which layout anchors to use.

Problem

If you’re using iOS 11’s safe area and NIBs for your interfaces the fallback constraints for iOS <=10 falls back to the superview’s anchors instead of top/bottom layout guides.

In a usual workflow you would setup your nib and looking OK in Interface Builder.

alt text

But in iOS <=10 it looks broken.

Actual Expected
alt text alt text

Install via Cocoapods

Add following to your Podfile:

use_frameworks!
pod 'CompatibleLayoutAnchors'

How to use

You have your layout in a NIB file ready with all layout constraints. Reference in your view controller the top constraints connected to the safe area top anchor and the bottom constraints connected to the safe area bottom anchor. Use assignCompatibleConstraint(_:for:) to reassign the top and bottom constraints with the appropriate constraints.

assignCompatibleConstraint(&myTopConstraint, for: .top)

Example

This code fits to the screenshots above.

class ViewController: UIViewController {

    @IBOutlet weak var headlineLabelTopConstraint: NSLayoutConstraint!
    @IBOutlet weak var footnoteLabelBottomConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()

        assignCompatibleConstraint(&headlineLabelTopConstraint, for: .top)
        assignCompatibleConstraint(&footnoteLabelBottomConstraint, for: .bottom)
    }
}

Contribution

Issues and pull requests are welcome!

Latest podspec

{
    "name": "CompatibleLayoutAnchors",
    "version": "2.0.2",
    "summary": "CompatibleLayoutAnchors takes away the boiler plate code to check which layout guide to use since safe area was introduced in iOS 11.",
    "authors": "Truffls GmbH",
    "license": {
        "type": "MIT"
    },
    "homepage": "https://github.com/truffls/compatible-layout-anchors-ios",
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/truffls/compatible-layout-anchors-ios.git",
        "tag": "2.0.2"
    },
    "source_files": "Pod/**/*.swift",
    "ios": {
        "frameworks": "UIKit"
    },
    "requires_arc": true
}

Pin It on Pinterest

Share This