Swiftpack.co - FelixHerrmann/FHConstraints as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
FelixHerrmann/FHConstraints
Extension for UIView with useful constraint methodes.
.package(url: "https://github.com/FelixHerrmann/FHConstraints.git", from: "2.2.0")

FHConstraints

Version License Tweet

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

link
Stars: 1
Last commit: 2 hours ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

2.2.0
6 weeks ago

CI workflow added

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics