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

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

Closed
hajinhoe opened this issue Mar 8, 2023 · 3 comments
Closed

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

hajinhoe opened this issue Mar 8, 2023 · 3 comments

Comments

@hajinhoe
Copy link

hajinhoe commented Mar 8, 2023

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.

@gmlwhdtjd
Copy link
Contributor

gmlwhdtjd commented Mar 10, 2023

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.

@hajinhoe
Copy link
Author

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

@gmlwhdtjd
Copy link
Contributor

Improved in PR #276.

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

No branches or pull requests

2 participants