Skip to content
lynx56Public

About

Small Auto Layout helpers for UIKit: add a subview and pin it with insets, fixed sizes and equal/greater/less constraints, including safe-area-aware bottom constraints. iOS 13+, CocoaPods.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GLayout

Small Auto Layout helpers for UIKit. Describe constraints as short key-path expressions, and add a subview with its constraints in one call. translatesAutoresizingMaskIntoConstraints is handled for you.

Installation

CocoaPods:

pod 'GLayout', '~> 1.0'

Swift Package Manager isn't supported yet. Requires iOS 13+ and Swift 5.

Usage

import GLayout

// Fill the parent with insets.
view.addSubview(card, constraints: Constraints.with(insets: UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)))

// Center with a fixed size.
view.addSubview(avatar,
                constraints: Constraints.center,
                singleConstraints: Constraints.fixedSize(size: CGSize(width: 64, height: 64)))

// Individual constraints; the second view defaults to the parent.
view.addSubview(button, constraints: [
    equal(\.leadingAnchor, constant: 20),
    equal(\.trailingAnchor, constant: -20),
    equal(\.bottomAnchor, \.safeAreaLayoutGuide.bottomAnchor, constant: -16)
])

// Relative to a sibling.
view.addSubview(title, constraints: [
    equal(\.topAnchor, to: avatar, \.bottomAnchor, constant: 8),
    equal(\.centerXAnchor),
    lessOrEqual(\.widthAnchor, constant: -40)
])

Building blocks

Function Makes
equal, greaterOrEqual, lessOrEqual A constraint between the view and its parent, or another view via to:. Pass a second key path to pin to a different anchor. Options: constant, priority, identifier, and multiplier for dimensions
equal(\.heightAnchor, constant: 44) A constant size (SingleConstraint)

Presets

Preset Pins
Constraints.pin / .with(insets:) All four edges
Constraints.with(top:bottom:left:right:) Only the edges you pass
Constraints.center Center X and Y
Constraints.fixedSize(size:) Width and height
RespectSafeAreaConstraints.pin / .with(insets:) Edges, top and bottom to the safe area
RespectBottomSafeAreaConstraints.pin / .with(insets:) Edges, bottom to the safe area

Existing views

spinner.setConstraints(Constraints.fixedSize(size: CGSize(width: 24, height: 24)))
badge.setConstraints(to: icon, constraints: [equal(\.topAnchor), equal(\.trailingAnchor)])

Finding a constraint later

button.setConstraints([equal(\.heightAnchor, identifier: "height", constant: 44)])
button.constraints["height"]?.constant = 56

License

MIT. See LICENSE.

About

Small Auto Layout helpers for UIKit: add a subview and pin it with insets, fixed sizes and equal/greater/less constraints, including safe-area-aware bottom constraints. iOS 13+, CocoaPods.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages