kean/Align

Missing multiplier parameter?

wow-such-amazing opened this issue · 2 comments

Hey!

First of all, really enjoying the library, thank you!

I would like to create a constraint like this:

$0.height.lessThanOrEqual(superview.anchors.height, multiplier: 2 / 3)

So that height of a view is 2/3 of a superview. However it's not possible to provide a multiplier. So I'm wondering if extension functions on AnchorType.Dimension miss a multiplier parameter?

Right now:

    /// Adds a constraint that defines the anchors' attributes as equal to each other.
    @discardableResult func equal<Type: AnchorType.Dimension, Axis>(_ anchor: Anchor<Type, Axis>, constant: CGFloat = 0) -> NSLayoutConstraint {
        Constraints.add(self, anchor, constant: constant, relation: .equal)
    }

    @discardableResult func greaterThanOrEqual<Type: AnchorType.Dimension, Axis>(_ anchor: Anchor<Type, Axis>, constant: CGFloat = 0) -> NSLayoutConstraint {
        Constraints.add(self, anchor, constant: constant, relation: .greaterThanOrEqual)
    }

    @discardableResult func lessThanOrEqual<Type: AnchorType.Dimension, Axis>(_ anchor: Anchor<Type, Axis>, constant: CGFloat = 0, multiplier: CGFloat = 1) -> NSLayoutConstraint {
        Constraints.add(self, anchor, constant: constant, relation: .lessThanOrEqual)
    }
}

With multiplier parameter:

public extension Anchor where Type: AnchorType.Dimension {
    /// Adds a constraint that defines the anchors' attributes as equal to each other.
    @discardableResult func equal<Type: AnchorType.Dimension, Axis>(_ anchor: Anchor<Type, Axis>, constant: CGFloat = 0, multiplier: CGFloat = 1) -> NSLayoutConstraint {
        Constraints.add(self, anchor, constant: constant, multiplier: multiplier, relation: .equal)
    }

    @discardableResult func greaterThanOrEqual<Type: AnchorType.Dimension, Axis>(_ anchor: Anchor<Type, Axis>, constant: CGFloat = 0, multiplier: CGFloat = 1) -> NSLayoutConstraint {
        Constraints.add(self, anchor, constant: constant, multiplier: multiplier, relation: .greaterThanOrEqual)
    }

    @discardableResult func lessThanOrEqual<Type: AnchorType.Dimension, Axis>(_ anchor: Anchor<Type, Axis>, constant: CGFloat = 0, multiplier: CGFloat = 1) -> NSLayoutConstraint {
        Constraints.add(self, anchor, constant: constant, multiplier: multiplier, relation: .lessThanOrEqual)
    }
}

Let me know if I missed something and behaviour that I would could be achieved in a different way 👀

For the moment I've implemented it without Align like this:

NSLayoutConstraint.activate([
    childView.heightAnchor.constraint(
        lessThanOrEqualTo: parentView.heightAnchor, multiplier: 2 / 3
    )
])

Cheers!

kean commented

Hey, I'm a little bit rusty, but AFAIR this is how it was originally designed to be used:

$0.height.lessThanOrEqual(superview.anchors.height * 0.66)

// OR in case you are not a fan of operator overload (the library adds very few)
$0.height.lessThanOrEqual(superview.anchors.height.multiplied(by: 0.66))

@kean Thanks for getting back with an answer! I will try it out in the future and re-open the issue if needed. Cheers!