Skip to main content

SnapKit

SnapKit is an Auto Layout DSL (Domain Specific Language) for iOS and OSX. It allows the use of written code to lay out views in Swift, as opposed to Apple's GUI-based Interface Builder tool. In iOS development, views are your UI elements and constraints are the relationships between views.

Overview

SnapKit, like all Auto Layout DSLs, acts as a layer of abstraction on top of Apple's Auto Layout.

This abstraction provides three main benefits:

  1. Simplifying the process of laying out your views
  2. Readable source control for your view layouts
  3. Syntactic sugar, reducing the amount of boilerplate code
  • The simplified layout process aids debugging layout issues, as the layout constraints themselves are human-readable. Laying out your views in code is less time-consuming, less error-prone, and less complicated than the built-in GUI layout offering.
  • The source control for SnapKit-created constraints is much more readable than the vanilla Interface Builder XML format. This enables effective collaboration in a team because developers can determine a view's constraints at a glance. This improved source control experience also helps resolve merge conflicts.
  • The syntactic sugar enables the creation of your constraints in code, in a readable format, without the excessive boilerplate code required for out-of-the-box Auto Layout programmatic constraints.

SnapKit adds a .snp attribute to all your view objects. This enables easy constraints between views, with constraints between a child view and its super view available in function form. The alternatives to SnapKit provide the same power but with a different stylistic feel. SnapKit's focus is on providing verbose and clear constraints.

SnapKit example code

SnapKit example code

Infinity Works

Pros

  • Lets you define views quickly and expressively in the code
  • Reduces boilerplate compared to using the vanilla constraints API
  • Avoids using Interface Builder, which is fiddly, time-consuming and error-prone
  • Constraints created this way are more reusable than GUI-created constraints
  • Popular and actively maintained, with over 16K stars on GitHub

Cons

  • You don't have a preview of the view, so you can't see what the layout will look like without running the project
  • Adds an extra dependency to your project