Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for system spacing in iOS 11 and High Sierra #44

Open
ZevEisenberg opened this issue Jun 5, 2017 · 8 comments
Open

Add support for system spacing in iOS 11 and High Sierra #44

ZevEisenberg opened this issue Jun 5, 2017 · 8 comments

Comments

@ZevEisenberg
Copy link
Collaborator

From the iOS 11 release notes:

Updated NSLayoutXAxisAnchor and NSLayoutYAxisAnchor 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()
@ZevEisenberg
Copy link
Collaborator Author

Also, if you're using baseline to baseline vertical constraints (i.e. Line spacing), the system spacing will take font size into account.

@ZevEisenberg
Copy link
Collaborator Author

img_1502

@mr-fixit
Copy link

mr-fixit commented Jun 9, 2017 via email

@ZevEisenberg
Copy link
Collaborator Author

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.

@ZevEisenberg
Copy link
Collaborator Author

Oops, I posted the "before" screenshot. Here's "After". It's from "What's New in Cocoa Touch" from WWDC 2017.
screen shot 2017-06-11 at 12 07 31 pm

@ZevEisenberg
Copy link
Collaborator Author

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?

@ZevEisenberg
Copy link
Collaborator Author

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:]

@jvisenti
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants