Pinnable

main

Convenient Auto Layout
kylebshr/Pinnable

Pinnable Header Image

Introduction

Pinnable provides convenient methods for creating NSLayoutConstraints - the foundation of working with Auto Layout. However, the standard UIKit APIs don’t offer enough defaults, and can also lead to frustrating results - for example, when you forget to set translatesAutoresizingMaskIntoConstraints.

Pinnable introduces a "single" method - pin - that offers sane defaults, disables translatesAutoresizingMaskIntoConstraints when needed, and automatically activates constraints that are created.

This is not a DSL, there is no "magic," and it doesn’t have fancy operator overloading. Because I don’t really like those things :) It’s just a little more convenient than the built-in APIs.

Installation

Pinnable is available as a Swift Package. Add it using the Xcode GUI, or add the following to your Package.swift:

.package(url: "https://github.com/kylebshr/Pinnable", from: "0.0.3")

Getting Started

After you’ve imported Pinnable, you’ll find new pin methods available on UIView, UILayoutGuide, NSLayoutAnchor, and NSLayoutDimension. Most of them are fairly self descriptive. For example, to constrain a view’s leading anchor to another view’s leading anchor, simply pin them together:

firstView.leadingAnchor.pin(to: secondView.leadingAnchor)

// With a constant
firstView.leadingAnchor.pin(to: secondView.leadingAnchor, constant: 20)

// ...and a priority
firstView.leadingAnchor.pin(to: secondView.leadingAnchor, constant: 20, priority: .defaultLow)

Or perhaps you’d like to pin all of the edges of the first view to second view:

firstView.pinEdges(to: secondView)

// With insets
firstView.pinEdges(to: secondView, insets: .init(top: 10, left: 20, bottom: 30, right: 20))

// ...or excluding the bottom edge
firstView.pinEdges([.top, .left, .right], to: secondView)

The methods available on UIView (and UILayoutGuide) are meant for creating constraints for multiple anchors (e.g., pinEdges can pin multiple edges, pinSize constrains both the width and height dimensions).

All of the pin methods return the constraints that were created, so you can reference them and mutate them however you’d like. Or feel free to ignore them. They don’t mind.

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

Last updated: Thu Mar 14 2024 07:42:28 GMT-0900 (Hawaii-Aleutian Daylight Time)