An AutoLayout library inspired by SnapKit.
CocodPods
Add this line to your Podfile
pod 'AnchorPal'
Manually
Add AnchorPal.xcodeproj
to your current project.
If you are familiar with SnapKit, you already know how to use AnchorPal, just with a little changes.
view.anc.installConstraints { the in
the.xEdges.equalToSuperview()
// with a optional parameter, you can easily refer to the layoutGuides of superview
the.yEdges.equalToSuperview(\.safeArea)
}
view.anc.reinstallConstraints { the in
the.height.equalTo(newValue)
}
let constaints = view.anc.makeConstraints { the in
the.edges.equalToSuperview(\.safeArea)
}
// Activate them later
constraints.activate()
Note: We don't use first.relationTo(second).inset(x)
format, because it's ambiguous.
When we say first.>=(second).inset(x)
, it can be "The first item has a greater value than second item insetting x.", but also can be "The inset from second item to first item is greater than x.". We want to be clear about the intent.
view.anc.installConstraints { the in
the.edges.insetFromSuperview().equalTo(30)
}
// or
view.anc.installConstraints { the in
the.edges.insetFrom(otherView).greaterEqualTo(30)
}
view.anc.installConstraints { the in
the.width.equalTo { position -> CGFloat in
// return a CGFloat as the new constant
// position is the info about the current anchor which is calling this closure
return newWidth
}
}
// update constants later
view.anc.updateConstraintConstants()
view1.anc.reinstallConstraints { the in
let space1 = the.trailing.spaceBefore(view2.anc.leading)
let space2 = the.bottom.spaceBefore(view3.anc.top)
space1.equalTo(space2)
}
// custom dimension to system spcaing
view1.anc.reinstallConstraints { the in
the.leading.spaceAfter(view2.anc.trailing).equalToSystemSpacing().multiply(2)
}
// or anchor to anchor
view1.anc.reinstallConstraints { the in
the.leading.equalToSystemSpacingAfter(view2.anc.trailing).multiply(2)
}
// Sometimes you just don't want to bind these constraints to any view.
Anc.installConstraints {
view1.anc.edges.equalToSuperview(\.safeArea)
view2.anc.top.equalTo(view1.anc.bottom).plus(20)
}
// make and install
view1.anc.edges.equalToSuperview(\.safeArea).active()
view2.anc.top.equalTo(view1.anc.bottom).plus(20).active()
// or just make
let constraint1 = view1.anc.edges.equalToSuperview(\.safeArea)
let constraint2 = view2.anc.top.equalTo(view1.anc.bottom).plus(20)
We change the lessThanOrEqualTo
to lessEqualTo
, and greaterThanOrEqualTo
to greaterEqualTo
, just be shorter, but still has no ambiguity.
AnchorPal is designed to be flexbile and readable, hope you can enjoy it, if don't, please feel free to tell us.