ioskrew/SwiftLayout

Asking for (x, y) settable anchor methods interface.

Closed this issue · 3 comments

Hello. I am recently using SwiftLayout, and familiar to SnapKit.
Using SwiftLayout, I have found (x, y) settable anchor's interface unfamiliar.

  1. When i using Anchor.center, It points to parent view defaultly. I don't predict it without comment. and dislike to other anchors, It get x, y value without AnchorsExpression. Honestly, I expected Anchors.center.equaltToSuper(x, y)
  2. And more, When i using size anchor, It doesn't set default parent anchor. It have different usability to avobe. If i need to size view equal to super view. I need to type like this Anchors.size(superView, x: 0, y: 0). I think it should be Anchors.size.equaltToSuper() if possible.

Could we make AnchorsExpression<AnchorsXYAxisAttribute> and
extension AnchorsExpression where Attribute == AnchorsXYAxisAttribute { func equaltToSuper(x, y) { ... } ... }

It's my opinion. Thanks.

Hello, first of all, thank you for your interest in SwiftLayout.


First, let's explain why this interface was developed.

Anchors interfaces has two main parts:

  • property interfaces like Anchors.centerX
  • method interfaces like Anchors.center(offsetX:, offsetY:)

For property interfaces, they are written to have a 1:1 mapping to NSLayoutConstraint.Attribute, so property interfaces that are not in NSLayoutConstraint.Attribute, such as the Anchors.center you mentioned, are not implemented, and syntax like Anchors.center.equaltToSuper(x:, y:) is not available. Also, Anchors.size as a property is not implemented for the same reason.

However, if I only implement the interface with a 1:1 mapping to NSLayoutConstraint.Attribute, you will have to enter the same words ["Anchors", "equalTo(constant:"] multiple times like this.

Anchors {
    Anchors.width.equalTo(constant: width)
    Anchors.height.equalTo(constant: height)
}

So, I also added an interface in the form of a method to reduce unnecessary repetition like below.

Anchors {
    Anchors.size(width: width, height: height)
}

Next, I'd like to positively review your suggestions for the following reasons.
  1. the 1:1 mapping of the property interface to NSLayoutConstraint.Attribute is not a big deal.
  2. as you mentioned, an interface like Anchors.center.equaltToSuper(x:, y:) would be less confusing than the current Anchors.center(offsetX:, offsetY:) and is very reasonable.
  3. if possible, it would be nice to replace all the interfaces of Anchors that are methods with property interfaces to make the syntax more uniform.

Thanks again for the great suggestions.

Awesome~ solved doubt! Yes. I also think developing it is possible, but also curious API design.

Improved in PR #276.