freshOS / Stevia

:leaves: Concise Autolayout code
https://freshos.github.io/SteviaDocs/
MIT License
3.38k stars 214 forks source link

Working with Safe Area Layout Guide and Operators #153

Closed edulpn closed 4 years ago

edulpn commented 4 years ago

I have recently migrated from Carthage to SPM and jumped from Stevia 4.7.3 to 5.1.0. I understand that this might be something related to major/breaking changes from version 4 to 5, but I used to have the following structure:

descriptionLabel.Top == safeAreaLayoutGuide.Top + 10.0
descriptionLabel.CenterX == safeAreaLayoutGuide.CenterX
descriptionLabel.Left == safeAreaLayoutGuide.Left + 10.0
descriptionLabel.Right == safeAreaLayoutGuide.Right - 10.0

Every time I try to operate either SteviaLayoutYAxisAnchor or SteviaLayoutXAxisAnchor (from safeAreaLayoutGuide.Top/Bottom/Left/Right) with a CGFloat I get the following error:

Binary operator '+'/'-' cannot be applied to operands of type 'SteviaLayoutYAxisAnchor' and 'CGFloat'

This doesn't happen if I try to operate between SteviaAttribute (from descriptionLabel.Top/Bottom/Left/Right) and CGFloat. This is probably by design, so what should be the way to handle Safe Area Layout Guides in this sense?

s4cha commented 4 years ago

Hi @edulpn, Thanks for reporting this. This looks like an issue on our side, SteviaLayoutYAxisAnchor operators work with a Double value. The others happen to have Int and CGFloat variants as well.

@available(iOS 9.0, *)
@discardableResult
public func + (left: SteviaLayoutYAxisAnchor, right: Double) -> SteviaLayoutYAxisAnchor {
    return SteviaLayoutYAxisAnchor(anchor: left.anchor, constant: right)
}

For now you could cast them to Double or just use a plain value, for example

viewA == safeAreaLayoutGuide.Top + 60

would work fine as the compiler would turn 60 as a Double.

This is definitely something we want to fix in a future release though.

Hope it helps,

edulpn commented 4 years ago

Hey @s4cha, thanks for the reply. Nice... Would you be open to receive a PR that implements Int and CGFloat operators? I can definitely do that if it helps to unburden you guys.

edulpn commented 4 years ago

I have forked the repo and implemented the extra operators, but I'm not sure about the tests. Guess I could open the PR and we communicate there?

s4cha commented 4 years ago

@edulpn of course, feel free to open the PR this is a great idea, nevermind if tests are not in there we'll work them out for you.

edulpn commented 4 years ago

@s4cha cool! I created the PR, let me know what file the tests should be and I can also add them.