Skip to content

Guide · Open source

How to use SnapKit: install and write Auto Layout code in Swift

Updated · written with AI from the project's own documentation and checked against our catalogue data

SnapKit is a Swift library that makes Apple's Auto Layout easier to write. Instead of long, verbose constraint code, you describe positions and sizes in a short chained syntax. This guide is for Swift developers building iOS, macOS or tvOS interfaces in code, and by the end you will know how to add SnapKit to a project and write a basic constraint.

SnapKitSnapKit/SnapKit · ★ 20.3k · MIT

Quick start

  1. 1Confirm your environment. Check that your project meets the README requirements: iOS 14.0+, macOS 12.0+ or tvOS 14.0+, Xcode 26.0+ and Swift 6.0+.
  2. 2Add the package dependency. Add the .package line with the SnapKit GitHub URL and .upToNextMajor(from: "6.0.0") to the dependencies value in Package.swift.
  3. 3Import SnapKit. Add import SnapKit at the top of each Swift file that sets constraints.
  4. 4Add your view. Create the view and call addSubview on its parent view before setting any constraints.
  5. 5Define constraints. Call snp.makeConstraints on the view and chain make.width, make.height, make.center or similar calls inside the closure.
  6. 6Build and run. Build the project in Xcode and check that the view appears where the constraints place it.

What SnapKit does

SnapKit is a DSL (domain-specific language) for Auto Layout. Auto Layout is Apple's system for positioning and sizing views based on constraints. SnapKit wraps that system in a more readable syntax, so the code that places your views is shorter and easier to follow.

It is aimed at developers building UIKit or AppKit interfaces in code rather than in Interface Builder. The README only covers code-based Auto Layout, so it is not the right tool if you build layouts visually in Storyboards or SwiftUI.

Check the requirements before you install

The current README lists these requirements: iOS 14.0+, macOS 12.0+ or tvOS 14.0+, Xcode 26.0+, and Swift 6.0+ (the toolchain, not the syntax). Check these against your project before you start.

If your project must run on older OS or Xcode versions, confirm compatibility first, because the README's stated requirements are the baseline.

  • iOS 14.0+ / macOS 12.0+ / tvOS 14.0+
  • Xcode 26.0+
  • Swift 6.0+ (toolchain)
  • Using Swift 5.x? The README says to use SnapKit 5.0.0 or later.

Install SnapKit with Swift Package Manager

The README recommends Swift Package Manager. Add SnapKit to the dependencies value of your Package.swift file. The version rule below accepts 6.0.0 and later within the same major version.

The README also mentions a manual integration option for projects that do not use a dependency manager, but it does not describe those steps.

dependencies: [ .package(url: "https://github.com/SnapKit/SnapKit.git", .upToNextMajor(from: "6.0.0")) ]

Write your first constraint

Import SnapKit in the file where you lay out views. Add the view to its parent with addSubview, then call snp.makeConstraints on the view. Inside the closure, chain the constraints you need.

The example below creates a 50 by 50 point view, centred in the parent view. The make.width.height.equalTo(50) call sets both dimensions at once, and make.center.equalTo(self.view) centres it.

  • import SnapKit at the top of the file
  • Add the view with addSubview before setting constraints
  • Use box.snp.makeConstraints { make in ... } to define the layout
import SnapKit class MyViewController: UIViewController { lazy var box = UIView() override func viewDidLoad() { super.viewDidLoad() self.view.addSubview(box) box.backgroundColor = .green box.snp.makeConstraints { (make) -> Void in make.width.height.equalTo(50) make.center.equalTo(self.view) } } }

Where to find more detail

The README links to the full documentation and an FAQ on the project homepage. Use these when you need syntax beyond the quick-start example.

For questions, the README points to Stack Overflow with the tag 'snapkit'. Bugs and feature requests go to the project's issue tracker, and contributions are submitted as pull requests.

  • Documentation: https://snapkit.github.io/SnapKit/docs/
  • F.A.Q.: https://snapkit.github.io/SnapKit/faq/
  • Questions: Stack Overflow, tag 'snapkit'

What people use it for

  • Centring and sizing a view in a UIViewController with a few chained lines
  • Replacing long NSLayoutConstraint blocks in an existing UIKit or AppKit project
  • Keeping Auto Layout code readable for a team that writes layouts in code

Frequently asked questions

Is SnapKit free to use?

Yes. The README says SnapKit is released under the MIT license, and the full text is in the LICENSE file in the repository.

What are the SnapKit requirements?

The README lists iOS 14.0+, macOS 12.0+ or tvOS 14.0+, Xcode 26.0+ and Swift 6.0+ (the toolchain). If you use Swift 5.x, the README says to use SnapKit 5.0.0 or later.

How do I install SnapKit?

The README recommends Swift Package Manager. Add the .package line with the SnapKit GitHub URL to the dependencies value of your Package.swift file.

Does SnapKit work with Storyboards or SwiftUI?

The README only covers code-based Auto Layout. If you build your interface in Storyboards or SwiftUI, SnapKit is not the focus of the project, so check whether it fits your workflow first.

What are alternatives to SnapKit?

The source names PinLayout, Masonry and TinyConstraints as comparable projects. The source does not compare them in detail, so review each one against your project needs.

Where can I get help with SnapKit?

The README suggests Stack Overflow with the tag 'snapkit' for help and general questions. Report bugs and request features by opening an issue on the project.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

SnapKit suits Swift developers who build UIKit or AppKit interfaces in code and want Auto Layout code that is shorter and easier to read, including teams maintaining existing SnapKit projects. Developers who lay out screens in Storyboards or SwiftUI, or who must support older OS or Xcode versions than the README lists, should look at alternatives such as PinLayout, Masonry or TinyConstraints.

Install options & downloads