explaingit

robb/cartography

7,325SwiftAudience · developerComplexity · 2/5Setup · easy

TLDR

A Swift library for iOS and macOS that replaces Apple's verbose Auto Layout API with short, math-like expressions so you can position UI elements on screen in one line instead of eight.

Mindmap

mindmap
  root((repo))
    What it does
      Auto Layout wrapper
      Swift library
      One-line constraints
    Features
      All layout attrs
      Constraint groups
      Animated transitions
    Platforms
      iOS
      macOS
    Setup
      CocoaPods
      Swift 5 support
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Position a button 12 points to the right of another button in your iOS app with one line instead of the eight-line native Auto Layout call

USE CASE 2

Animate a layout change by swapping a named constraint group for a new one and triggering a UIView animation block

USE CASE 3

Distribute a row of buttons at equal spacing across the screen with a single Cartography function call

Tech stack

SwiftiOSmacOSCocoaPods

Getting it running

Difficulty · easy Time to first run · 30min

Install via CocoaPods, use version 4.x or above for Swift 5 compatibility.

In plain English

Cartography is a Swift library for iOS and macOS developers that makes positioning user interface elements on screen much less verbose. On Apple platforms, views (buttons, images, text labels, and so on) are sized and placed using a system called Auto Layout, where you define rules describing how elements should relate to each other in size and position. The native Auto Layout API requires a long, repetitive call for each rule. Cartography replaces this with a shorter, more readable style where constraints are written as familiar math-like expressions inside a small function call. For example, saying that one button's right edge should sit 12 points to the left of another button's left edge takes about 8 lines with the native API. With Cartography the same rule fits on one line. The library supports all standard layout attributes built into iOS and macOS: width, height, top, bottom, left, right, center, baseline, and the various margin and alignment variants. Compound shortcuts let you set multiple attributes at once, such as matching an element's full size to another view or placing it at its parent's center. Aligning a group of views along a shared edge, or distributing several views at even spacing, each require a single call. Constraints can be captured into named groups and then replaced later with new constraints, which makes animated layout transitions straightforward. A shorthand operator lets you attach a priority value to any rule, which the system uses to decide which constraint to honor when two rules conflict. Installation is via CocoaPods. Version compatibility follows Swift releases: versions 1.x target Swift 3, versions 2.x target Swift 4, and versions 4.x and above target Swift 5.

Copy-paste prompts

Prompt 1
I'm building an iOS app in Swift and want to use Cartography to position a label 20 points below an image view and centered horizontally. Show me how to add Cartography via CocoaPods and write the constrain block.
Prompt 2
I have two views in my iOS app and I want their widths to always match each other using Cartography. Show me how to write that constraint and how to update it later to use a different width ratio with an animated transition.
Prompt 3
I have five buttons I want to spread evenly across the bottom of my iOS screen using Cartography. Show me the Swift code to distribute them at equal horizontal spacing.
Open on GitHub → Explain another repo

← robb on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.