Extension for UIView with useful constraint methodes

FHConstraints

FHConstraints is an extension for the UIView class with useful constraint methods.
Theses methods simplify the build-in constraint methods.

Requirements

  • macOS 10.11
  • macOS 10.15+ (Catalyst)
  • iOS 9.0+
  • tvOS 9.0+

Installation

Swift Package Manager

Add the following to the dependencies of your Package.swift:

.package(url: "https://github.com/FelixHerrmann/FHConstraints.git", from: "x.x.x")

Manual

Download the files in the Sources folder and drag them into you project.

Usage

If you are using Swift Package Manager, you have to import FHConstraints to your file with import FHConstraints.
You can also import it globally with @_exported import FHConstraints. (e.g. in AppDelegate.swift)


Instead of:

subview.translatesAutoresizingMaskIntoConstraints = false
subview.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
subview.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: -10).isActive = true
subview.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 5).isActive = true
subview.heightAnchor.constraint(greaterThanOrEqualTo: view.heightAnchor, multiplier: 1.5, constant: 20).isActive = true

you will use:

subview.constraint(.leading(to: view.leadingAnchor))
subview.constraint(.trailing(to: view.trailingAnchor, .lessThanOrEqual(to: -10)))
subview.constraint(.centerY(to: view.centerYAnchor, .equal(to: 5)))
subview.constraint(.height(to: view.heightAnchor, .greaterThanOrEqual(to: FHLayoutAnchor.DimensionConstant(multiplier: 1.5, constant: 20))))

All of these methods will return the created constraint.

Convenience Methods

Combined constraining method:

subview.constraint([
    .leading(to: view.leadingAnchor),
    .trailing(to: view.trailingAnchor, .lessThanOrEqual(to: -10)),
    .centerY(to: view.centerYAnchor, .equal(to: 5)),
    .height(to: view.heightAnchor, .greaterThanOrEqual(to: FHLayoutAnchor.DimensionConstant(multiplier: 1.5, constant: 20)))
])

Shortcut methods:

subview.constraint(.inside(of: view, insets: .equal(to: EdgeInsets(top: 0, left: 0, bottom: 0, right: 0))))
subview.constraint(.toCenter(of: view, offset: .equal(to: FHConvenienceAnchors.Offset(horizontal: 0, vertical: 0))))
subview.constraint(.size(.equal(to: CGSize(width: 0, height: 0)))

Constraint creation (but not activation) method:

let leadingConstraint = subview.createConstraint(from: .leading(to: view.leadingAnchor))

License

FHConstraints is available under the MIT license. See the LICENSE file for more info.

GitHub

https://github.com/FelixHerrmann/FHConstraints