A friendly introduction to SnapKit and it’s benefits over adding standard programmatic constraints.

Written while using XCode 12.4 / SnapKit: 5.0.1

Filmstack Project created with SnapKit.

Full project can be found on my Github

I. Overview

Snapkit is, “… a DSL to make Auto Layout easy on both iOS and OS X.”
In other words, it’s a library to abstract auto layout and simplify the workflow for programmatic constraints.

II. Benefits

So why use SnapKit? Well, some of the benefits include:

  • Less Code
  • Easier to read / learn
  • Widely Used

Less Code

Constraining a TableView with standard AutoLayout Constraints
Constraining a TableView with SnapKit

Easier

SnapKit is arguably easier to use than auto layout. There’s less steps to account for and it reads better.

As a beginner, setting up views with code is tedious because you need to have a good foundation on what the UIKit object library has, how to setup the project to not use Storyboards and finally learn ways to actually layout a view with programmatic auto layout. This was the case for me when I was first learning. I would constantly reference a Storyboard to view the object library and would always forget a step. These two were usually the culprits:

  • translatesAutoresizingMaskIntoConstraints = false
  • .isActive = true

If you need help setting up a programmatic project, I have another article with more resources: XCode Progammatic Project Setup ! 😉

Widely Used

This point isn’t as important because if SnapKit makes YOUR workflow better then use it or don’t, but it is worth noting that SnapKit is open source and actively contributed to. It’s safe to say it will be maintained for the near future. Even companies like Github use SnapKit in their iOS app.

III. Set up

If you have added a third party library before, SnapKit’s installation guide on their Github repo is straightforward.

If this is your first time, I recommend using Swift Package Manager if possible to add SnapKit. It’s quick and can be done within Xcode.

Swift Package Manager: Adding a Package Repository
import SnapKit at the top of file

After adding SnapKit, import it at the top of every file you’re constraining and you’re good to go.

IV. Example

Here is an example of how I used SnapKit to layout this container view from an upcoming project. The full project can be found on my Github.

V. Summary

If you are starting to learn programmatic constraints and finding it difficult to understand, SnapKit can help minimize the learning curve and requires much less code compared to the standard way of adding programmatic constraints.

Regardless, I still highly recommend learning the standard programmatic constraints once you are comfortable with SnapKit because there is value in understanding how auto layout constraints work.

SnapKit was created by: Robert Payne and open source contributers.

Resources for SnapKit:

mobile software engineer \\\ ☕️💻 📷 https://github.com/leoidiaz

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store