AutoLayoutBuilder 2.0.0

AutoLayoutBuilder 2.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Nov 2015
SPMSupports SPM

Maintained by Marc Baldwin.



  • By
  • marcbaldwin

AutoLayoutBuilder

A DSL for Apple’s Auto-Layout written in Swift

Examples | Installation

Why use AutoLayoutBuilder?

  • Expressive - Constraints are extremely readable and concise
  • Fast - Write fewer lines of code, and shorter ones at that
  • Safe - Invalid constraints are compiler errors
  • Flexible - Attributes are supplied as arguments, so can be varied at runtime

Examples

// Example: Left equals right
let constraints = view1[.Left] == view2[.Right]

// Example: Center X of view1, view2 and view3 equal to center X of view4
let constraints = Group(view1, view2, view3)[.CenterX] == view4[.CenterX]

// Example: Left equals left with margin
let constraints = view1[.Left] == view2[.Left] + 10

// Example: Equal X and Y centers
let constraints = view1[.Center] == view2[.Center]

// Example: Equal X centers with margin
let constraints = view1[.CenterX] == view2[.CenterX] - 10

// Example: CenterY equals Top with margin
let constraints = view1[.CenterY] == view2[.Top] - 10

// Example: Equal edges
let constraints = view1[.Edges] == view2[.Edges]

// Example: Equal edges with margin
let constraints = view1[.Edges] == view2[.Edges] - 5

// Example: Equal sizes
let constraints = view1[.Size] == view2[.Size]

// Example: Greater than or equal sizes
let constraints = view1[.Size] >= view2[.Size]

// Example: Relative sizes
let constraints = view1[.Size] == view2[.Size] * 0.5

// Example: Fixed size
let constraints = view1[.Size] == CGSize(width: 50, height: 20)

// Example: Equal widths
let constraints = view1[.Width] == view2[.Width]

// Example: Equal width and height
let constraints = view1[.Width] == view1[.Height]

// Example: Left to right layout
let constraints = .Horizontal ~ view1 | view2 | view3

// Example: Left to right layout with margins
let constraints = .Horizontal ~ view1 | 5 | view2 | 5 | view3

// Example: Leading to trailing layout with margins
let constraints = .LeadingToTrailing ~ view1 | 5 | view2 | 5 | view3

// Example: Top to bottom layout with margins
let constraints = .Vertical ~ view1 | 5 | view2 | 5 | view3

// Example: Top equals top layout guide's bottom
let constraints = view1[.Top] == viewController.layoutGuide.top[.Bottom]

// Example: Bottom equals bottom layout guide's top with margin
let constraints = view1[.Bottom] == viewController.layoutGuide.bottom[.Top] - 5

Installation