A Swift Autolayout Library for iOS, tvOS and macOS.
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)
}// 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)
}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)
}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))
}box.af.constraints {
$0.width.equal(to: superview, options: .multiplier(0.5), .priority(750))
$0.height.equal(toOptions: .constant(44), .identifier("box.height"))
}box.af.constraints {
$0.width.equal(toConstant: 200)
_ = $0.aspectRatio(9.0 / 16.0) // height = width * 9/16
}// 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
}// 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)
}box.af.constraints {
$0.left.equal(to: superview, constant: 16)
$0.right.equal(to: superview, constant: 16)
}// 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))
}// Uses leading/trailing instead of left/right
box.af.constraints {
$0.edges.equal(directionalInsets: NSDirectionalEdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
}// Toggle constraints on/off (useful for show/hide patterns)
box.af.deactivate() // Deactivates all stored constraints
box.af.activate() // Reactivates them// Shorthand for setContentHuggingPriority / setContentCompressionResistancePriority
label.af.setHugging(.required, for: .horizontal)
label.af.setCompressionResistance(.defaultLow, for: .vertical)// Pin to superview's safe area layout guide
box.af.constraints {
$0.top.equalToSafeArea(constant: 8)
$0.leading.trailing.equalToSafeArea()
$0.bottom.equalToSafeArea()
}// 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 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 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)")
}- iOS 13.0+
- tvOS 13.0+
- macOS 11.0+
- visionOS 1.0+
- Xcode 14.1+
- Swift 5.7.1+
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")
])
]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
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'
endAnd run pod install.
Important
CocoaPods 1.13.0 or newer is required.
If you're using Carthage, add this to your Cartfile:
github "liam-i/AutoFlex" ~> 0.4.0And run carthage update --platform iOS --use-xcframeworks.
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.
AutoFlex is available under the MIT license. See the LICENSE file for more info.