talira

SnapKit

A Swift DSL that makes Auto Layout easy on iOS and macOS

open sourceMITfree

SnapKit is a Swift DSL for writing Auto Layout constraints in code: instead of building NSLayoutConstraint objects by hand, you write view.snp.makeConstraints { make in make.edges.equalToSuperview() } and chain sizes, edges, centers, offsets and insets. It's type-safe, so invalid constraints can't be created, and it can update, remake or remove a view's constraints later. Version 6 needs iOS 14, macOS 12 or tvOS 14 and installs with Swift Package Manager.

Problems it solves

  • writing NSLayoutConstraint code by hand in my iOS app is verbose and hard to read
  • lay out UIKit views in code without storyboards or Interface Builder
  • pin a view to its superview's edges with insets in one line of Swift
  • update or remake a view's Auto Layout constraints when the screen changes

Features

  • Chaining DSL for Auto Layout constraints
  • Type-safe constraints
  • Make, update, remake and remove constraints
  • iOS, macOS and tvOS