Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsMay 17, 2026·2 min de lecture

SnapKit — Auto Layout DSL for iOS and macOS

A Swift DSL that makes Auto Layout constraints easy and readable, replacing verbose NSLayoutConstraint code with a concise chainable syntax for iOS and macOS development.

Prêt pour agents

Cet actif peut être lu et installé directement par les agents

TokRepo expose une commande CLI universelle, un contrat d'installation, le metadata JSON, un plan selon l'adaptateur et le contenu raw pour aider les agents à juger l'adaptation, le risque et les prochaines actions.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
SnapKit Overview
Commande CLI universelle
npx tokrepo install fd6453fc-520b-11f1-9bc6-00163e2b0d79

Introduction

SnapKit is a Swift DSL (Domain Specific Language) that simplifies Apple's Auto Layout API. Instead of writing verbose NSLayoutConstraint code or wrestling with Interface Builder, developers express layout constraints using a readable, chainable closure syntax that reduces boilerplate and makes programmatic UI layout maintainable.

What SnapKit Does

  • Provides a closure-based DSL for creating Auto Layout constraints in code
  • Supports all constraint attributes: edges, size, center, baseline, and margins
  • Allows updating and remaking constraints with snp.updateConstraints and snp.remakeConstraints
  • Handles translatesAutoresizingMaskIntoConstraints automatically
  • Works with both UIKit (iOS/tvOS) and AppKit (macOS) views

Architecture Overview

SnapKit wraps each UIView/NSView with a ConstraintViewDSL (accessed via .snp) that creates a ConstraintMaker inside a closure. Each chained call builds a ConstraintDescription with target attribute, relation, and constant. When the closure completes, descriptions are compiled into actual NSLayoutConstraint objects and activated on the appropriate view in the hierarchy.

Self-Hosting & Configuration

  • Install via Swift Package Manager or CocoaPods: pod 'SnapKit', '~> 5.7'
  • Import SnapKit in any file where you build constraints programmatically
  • Use snp.makeConstraints for initial setup in viewDidLoad or init
  • Call snp.updateConstraints inside animation blocks for animated layout changes
  • Enable debug labels with snp.makeConstraints to trace unsatisfiable constraint logs

Key Features

  • Chainable syntax: make.top.leading.trailing.equalToSuperview()
  • Priority support via .priority(.high) or custom numeric values
  • Offset and inset helpers for common padding patterns
  • Multiplier support for proportional sizing: make.width.equalToSuperview().multipliedBy(0.5)
  • Safe area and layout margin guide support built-in

Comparison with Similar Tools

  • NSLayoutConstraint (native) — verbose and error-prone; SnapKit reduces constraint code by 60-80%
  • Interface Builder — visual but hard to review in git; SnapKit keeps layouts in code
  • Cartography — similar DSL using operator overloading; SnapKit uses closures for clarity
  • SwiftUI Layout — declarative but UIKit-only apps still need SnapKit for programmatic constraints

FAQ

Q: Can I animate constraint changes with SnapKit? A: Yes, call snp.updateConstraints inside UIView.animate and then call layoutIfNeeded().

Q: Does SnapKit work with UIScrollView? A: Yes, constrain the content view edges to the scroll view's contentLayoutGuide for proper scrolling.

Q: How do I debug unsatisfiable constraints? A: SnapKit prints readable labels in console logs. Add .labeled("myConstraint") for custom identifiers.

Q: Is SnapKit compatible with SwiftUI? A: SnapKit is for UIKit/AppKit views. In mixed apps, use UIViewRepresentable to bridge SnapKit-based views into SwiftUI.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires