AceLayout provides a Swifty DSL for Auto Layout.
some.top == another.leading
.view.translatesAutoresizingMaskIntoConstraints = false
constraint.isActive = true
Call the autoLayout
method of your UIView
, UILayoutGuide
, NSView
or NSLayoutGuide
with a closure that describes Auto Layout constraints.
view.autoLayout { item in
item.top.equal(to: anotherView, plus: 16) // to UIView
item.bottom.equalToSuperview() // to superview
item.leading.equal(to: layoutMarginsGuide) // to UILayoutGuide
item.trailing.equal(to: anotherView.centerXAnchor) // to NSLayoutAnchor
item.width.equal(to: 100) // to constant
item.height.equal(to: item.width) // to LayoutAnchor
}
In the example, the followings are done automatically:
Set view.translatesAutoresizingMaskIntoConstraints
to false
.
Activates all constraints.
view.autoLayout { item in
item.top.greaterThanOrEqual(to: safeAreaLayoutGuide)
item.bottom.lessThanOrEqualToSuperview()
}
UILayoutPriority
and NSLayoutConstraint.Priority
are available.
view.autoLayout { item in
item.center.equalToSuperview().priority(.required)
item.size.equal(toSquare: 100).priority(.defaultHigh)
}
You can specify an argument activates
to determine whether to immediately activate constraints.
let constraints = view.autoLayout(activates: false) { item in
item.edges.equal(to: anotherView)
}
// All constraints are not active.
assert(constraints.allSatisfy { !$0.isActive })
// You can activate them at any time.
NSLayoutConstraint.activate(constraints)
view.autoLayout { item in
item.center.equal(to: anotherView)
item.topLeading.equalToSuperview()
}
view.autoLayout { item in
item.size.equal(to: CGSize(width: 100, height: 200))
}
view.autoLayout { item in
item.leadingTrailing.equal(to: anotherView)
item.topBottom.equalToSuperview(insetBy: 16)
}
view.autoLayout { item in
item.edges.equalToSuperview(insetBy: 16)
}
leading
trailing
left
right
centerX
top
bottom
centerY
UIView
/ NSView
)firstBaseline
lastBaseline
width
height
center
topLeading
topTrailing
topLeft
topRight
bottomLeading
bottomTrailing
bottomLeft
bottomRight
size
leadingTrailing
leftRight
topBottom
edges
To use the AceLayout
library in a SwiftPM project, add the following line to the dependencies in your Package.swift
file:
.package(url: "https://github.com/jrsaruo/AceLayout", from: "1.1.2"),
and add AceLayout
as a dependency for your target:
.target(name: "<target>", dependencies: [
.product(name: "AceLayout", package: "AceLayout"),
// other dependencies
]),
Finally, add import AceLayout
in your source code.
link |
Stars: 2 |
Last commit: 2 weeks ago |
ExistentialAny
by @jrsaruo in https://github.com/jrsaruo/AceLayout/pull/28Full Changelog: https://github.com/jrsaruo/AceLayout/compare/1.1.1...1.1.2
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics