Skip to content

Automatically apply top/bottom layout anchors without having to check safe area compatibility

License

Notifications You must be signed in to change notification settings

truffls/compatible-layout-anchors-ios

Repository files navigation

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!

About

Automatically apply top/bottom layout anchors without having to check safe area compatibility

Resources

License

Stars

Watchers

Forks

Packages

No packages published