Skip to content

liam-i/AutoFlex

Repository files navigation

AutoFlex

Swift Platforms CocoaPods SPM Carthage License

A Swift Autolayout Library for iOS, tvOS and macOS.

Usage

Basic Constraints

import AutoFlex

view.addSubview(box)
box.af.constraints {
    $0.top.equal(to: view, constant: 20)
    $0.leading.trailing.equal(to: view, constant: 16)
    $0.height.equal(toConstant: 44)
}

Compound Attributes

// Pin all edges with 12pt inset
box.af.constraints {
    $0.edges.equal(to: superview, constant: 12)
}

// Center + size
box.af.constraints {
    $0.center.equal(to: superview)
    $0.size.equal(to: CGSize(width: 100, height: 50))
}

// Horizontal/vertical edges
box.af.constraints {
    $0.horizontalEdges.equal(to: superview, constant: 16)
    $0.verticalEdges.equal(to: superview, constant: 8)
}

Relations

box.af.constraints {
    // Positive values = inward distance
    $0.top.greaterOrEqual(to: superview, constant: 8)       // at least 8pt from top
    $0.trailing.greaterOrEqual(to: superview, constant: 12) // at least 12pt from right
    $0.width.lessOrEqual(toConstant: 300)
    $0.height.greaterOrEqual(toConstant: 44)
}

Edge Insets

box.af.constraints {
    $0.edges.equal(to: superview, insets: UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16))
}

// greaterOrEqual/lessOrEqual with insets
box.af.constraints {
    $0.edges.greaterOrEqual(to: superview, insets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
}

Options (constant / multiplier / priority / identifier)

box.af.constraints {
    $0.width.equal(to: superview, options: .multiplier(0.5), .priority(750))
    $0.height.equal(toOptions: .constant(44), .identifier("box.height"))
}

Aspect Ratio

box.af.constraints {
    $0.width.equal(toConstant: 200)
    _ = $0.aspectRatio(9.0 / 16.0) // height = width * 9/16
}

Remake & Update Constraints

// Deactivate old constraints and create new ones
box.af.remakeConstraints {
    $0.edges.equal(to: superview, constant: 20)
}

// Modify existing constraint constants
box.af.updateConstraints { constraints in
    constraints.first { $0.identifier == "box.height" }?.constant = 60
}

Anchor Accessors

// Direct anchor access via .af
let topAnchor = view.af.top           // NSLayoutYAxisAnchor
let guide = view.af.safeGuide         // UILayoutGuide
let margins = view.af.marginsGuide    // UILayoutGuide

// Use as constraint target
box.af.constraints {
    $0.top.equal(to: otherView.af.bottom, constant: 8)
    $0.leading.trailing.equal(to: view.af.safeGuide)
}

Left / Right (RTL-unaware)

box.af.constraints {
    $0.left.equal(to: superview, constant: 16)
    $0.right.equal(to: superview, constant: 16)
}

Superview Convenience

// No need to pass the superview explicitly
box.af.constraints {
    $0.top.leading.trailing.equalToSuperview(constant: 16)
    $0.height.equal(toConstant: 44)
}

// Also supports greaterOrEqual / lessOrEqual / options
box.af.constraints {
    $0.leading.greaterOrEqualToSuperview(constant: 12)
    $0.width.equalToSuperview(options: .multiplier(0.8))
}

Directional Edge Insets

// Uses leading/trailing instead of left/right
box.af.constraints {
    $0.edges.equal(directionalInsets: NSDirectionalEdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
}

Activate / Deactivate

// Toggle constraints on/off (useful for show/hide patterns)
box.af.deactivate()   // Deactivates all stored constraints
box.af.activate()     // Reactivates them

Content Priority

// Shorthand for setContentHuggingPriority / setContentCompressionResistancePriority
label.af.setHugging(.required, for: .horizontal)
label.af.setCompressionResistance(.defaultLow, for: .vertical)

Safe Area Convenience

// Pin to superview's safe area layout guide
box.af.constraints {
    $0.top.equalToSafeArea(constant: 8)
    $0.leading.trailing.equalToSafeArea()
    $0.bottom.equalToSafeArea()
}

Center In Superview

// Center the view in its superview with optional offsets
box.af.constraints {
    $0.centerInSuperview()
}

// With offsets
box.af.constraints {
    $0.centerInSuperview(xOffset: 10, yOffset: -20)
}

Pin To Superview Edges

// Pin all edges except the specified ones
box.af.constraints {
    $0.pinToSuperviewEdges(excluding: .bottom, constant: 16)
}

// Exclude multiple edges
box.af.constraints {
    $0.pinToSuperviewEdges(excluding: [.top, .bottom], constant: 8)
}

Animate Constraint Changes

// Animate after modifying constraints
box.af.updateConstraints { constraints in
    constraints.first { $0.identifier == "box.height" }?.constant = 100
}
box.af.animate(duration: 0.3)

// With completion handler
box.af.animate(duration: 0.25) { finished in
    print("Animation done: \(finished)")
}

Requirements

  • iOS 13.0+
  • tvOS 13.0+
  • macOS 11.0+
  • visionOS 1.0+
  • Xcode 14.1+
  • Swift 5.7.1+

Swift Package Manager

...using swift build

If you are using the Swift Package Manager, add a dependency to your Package.swift file and import the AutoFlex library into the desired targets:

dependencies: [
    .package(url: "https://github.com/liam-i/AutoFlex.git", from: "0.4.0")
],
targets: [
    .target(
        name: "MyTarget", dependencies: [
            .product(name: "AutoFlex", package: "AutoFlex")
        ])
]

...using Xcode

If you are using Xcode, then you should:

  • File > Swift Packages > Add Package Dependency
  • Add https://github.com/liam-i/AutoFlex.git
  • Select "Up to Next Minor" with "0.4.0"

Tip

For detailed tutorials, see: Apple Docs

CocoaPods

If you're using CocoaPods, add this to your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
# Or use CND source
# source 'https://cdn.cocoapods.org/'
platform :ios, '13.0'
use_frameworks!

target 'MyApp' do
  pod 'AutoFlex', '~> 0.4.0'
end

And run pod install.

Important

CocoaPods 1.13.0 or newer is required.

Carthage

If you're using Carthage, add this to your Cartfile:

github "liam-i/AutoFlex" ~> 0.4.0

And run carthage update --platform iOS --use-xcframeworks.

Example

To run the example project, first clone the repo, then cd to the root directory and run pod install. Then open AutoFlex.xcworkspace in Xcode.

License

AutoFlex is available under the MIT license. See the LICENSE file for more info.

About

A Swift Autolayout Library for iOS & tvOS & macOS.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors