Anchorage
Anchorage copied to clipboard
Add support for system spacing in iOS 11 and High Sierra
From the iOS 11 release notes:
Updated
NSLayoutXAxisAnchor
andNSLayoutYAxisAnchor
to provide factory methods that create constraints using the system spacing between two anchors. Previously the only way to create such a constraint was with the dash (-
) in the Visual Format Language.
Anchorage should add some kind of syntactic sugar or named CGFloat
value like .system
or something. The new API looks like this:
func constraintEqualToSystemSpacingAfter(NSLayoutXAxisAnchor, multiplier: CGFloat)
It might be nice to use it like this:
view2.trailingAnchor == view1.leadingAnchor + SystemSpacing()
Also, if you're using baseline to baseline vertical constraints (i.e. Line spacing), the system spacing will take font size into account.
Did the system spacing change between the three examples of “Lunch with Josh”? Or are they of different sizes for another reason.
If you use the system spacing on a vertical constraint from baseline to baseline of two labels, that spacing will dynamically adjust based on Dynamic Type size.
Oops, I posted the "before" screenshot. Here's "After". It's from "What's New in Cocoa Touch" from WWDC 2017.
Here's a little more info. The constraintEqualToSystemSpacing
method isn't in the API diffs as of this writing, but here it is from the generated Swift header:
extension NSLayoutYAxisAnchor {
/* Constraints of the form,
receiver [= | ≥ | ≤] 'anchor' + 'multiplier' * system space,
where the value of the system space is determined from information available from the anchors.
The constraint affects how far the receiver will be positioned below 'anchor'.
If either the receiver or 'anchor' is the firstBaselineAnchor or lastBaselineAnchor of a view with text content
then the spacing will depend on the fonts involved and will change when those do.
*/
@available(iOS 11.0, *)
open func constraintEqualToSystemSpacingBelow(_ anchor: NSLayoutYAxisAnchor, multiplier: CGFloat) -> NSLayoutConstraint
@available(iOS 11.0, *)
open func constraintGreaterThanOrEqualToSystemSpacingBelow(_ anchor: NSLayoutYAxisAnchor, multiplier: CGFloat) -> NSLayoutConstraint
@available(iOS 11.0, *)
open func constraintLessThanOrEqualToSystemSpacingBelow(_ anchor: NSLayoutYAxisAnchor, multiplier: CGFloat) -> NSLayoutConstraint
}
I believe the doc comment has swapped the 'multiplier'
and system space
values, but otherwise it gives a sense of how the API is used. I'm thinking that Anchorage may need some under-the-hood changes so that it can dynamically pick which methods to use based on the equation, but I think it will be possible.
Also, note that this is possible only on the Y axis, since it's apparently only used with text line spacing. Can our expression system encode that limitation?
Oops, I think I was wrong about y-axis-only. According to these generated diffs, there are x and y variants of the system spacing methods:
Added -[NSLayoutXAxisAnchor anchorWithOffsetToAnchor:]
Added NSLayoutXAxisAnchor (UIViewDynamicSystemSpacingSupport)
Added -[NSLayoutXAxisAnchor constraintEqualToSystemSpacingAfterAnchor:multiplier:]
Added -[NSLayoutXAxisAnchor constraintGreaterThanOrEqualToSystemSpacingAfterAnchor:multiplier:]
Added -[NSLayoutXAxisAnchor constraintLessThanOrEqualToSystemSpacingAfterAnchor:multiplier:]
Added -[NSLayoutYAxisAnchor anchorWithOffsetToAnchor:]
Added NSLayoutYAxisAnchor (UIViewDynamicSystemSpacingSupport)
Added -[NSLayoutYAxisAnchor constraintEqualToSystemSpacingBelowAnchor:multiplier:]
Added -[NSLayoutYAxisAnchor constraintGreaterThanOrEqualToSystemSpacingBelowAnchor:multiplier:]
Added -[NSLayoutYAxisAnchor constraintLessThanOrEqualToSystemSpacingBelowAnchor:multiplier:]
Interesting that this is the first time a multiplier is supported for a spacing constraint. Do we know if a multiplier version of all spacing constraints will be available in iOS 11, or just this one?