Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't programmatically set NSLayoutConstraint multiplier in swift... "Cannot assign to the result of this expression

I am trying to programmatically set the constraint for a multiplier in swift, and when I set the value, it just gives me the error, "Cannot assign to the result of this expression"...

I declared the NSLayoutConstraint with an IBOutlet, and then set the multiplier, same as I did with the constant for another, which works fine, but this one won't accept it...

@IBOutlet weak var clockWidthConstraint: NSLayoutConstraint!

override func updateViewConstraints() {
    super.updateViewConstraints()

    confirmTopConstraint.constant = 40.0
    clockWidthConstraint.multiplier = 0.1  // Gives me the error!

    }

Any ideas?

like image 456
Jake Weso Avatar asked Jan 09 '15 22:01

Jake Weso


1 Answers

Yeah, it's read only:

var multiplier: CGFloat { get } 

You can only specify the multiplier at creation time. In contrast, you can change the non-constant constant property at run-time:

var constant: CGFloat

Edit:

It takes a little effort, but to change immutable properties, you have to create a new constraint and copy all but the property that you want to change. I keep playing around with different techniques for this, but am currently exploring this form:

let constraint = self.aspectRatioConstraint.with() {
    (inout c:NSLayoutConstraint.Model) in
    c.multiplier = self.aspectRatio }

or used in a more realistic context:

var aspectRatio:CGFloat = 1.0 { didSet {

    // remove, update, and add constraint
    self.removeConstraint(self.aspectRatioConstraint)
    self.aspectRatioConstraint = self.aspectRatioConstraint.with() {
        (inout c:NSLayoutConstraint.Model) in
        c.multiplier = self.aspectRatio }
    self.addConstraint(self.aspectRatioConstraint)
    self.setNeedsLayout()
}}

Where the backing code is:

extension NSLayoutConstraint {

    class Model {

        init(item view1: UIView, attribute attr1: NSLayoutAttribute,
            relatedBy relation: NSLayoutRelation,
            toItem view2: UIView?, attribute attr2: NSLayoutAttribute,
            multiplier: CGFloat, constant c: CGFloat,
            priority:UILayoutPriority = 1000) {
                self.firstItem = view1
                self.firstAttribute = attr1
                self.relation = relation
                self.secondItem = view2
                self.secondAttribute = attr2
                self.multiplier = multiplier
                self.constant = c
                self.priority = priority
        }

        var firstItem:UIView
        var firstAttribute:NSLayoutAttribute
        var relation:NSLayoutRelation
        var secondItem:UIView?
        var secondAttribute:NSLayoutAttribute
        var multiplier:CGFloat = 1.0
        var constant:CGFloat = 0
        var priority:UILayoutPriority = 1000
    }


    func priority(priority:UILayoutPriority) -> NSLayoutConstraint {
        self.priority = priority;
        return self
    }

    func with(configure:(inout Model)->()) -> NSLayoutConstraint {

        // build and configure model
        var m = Model(
            item: self.firstItem as! UIView, attribute: self.firstAttribute,
            relatedBy: self.relation,
            toItem: self.secondItem as? UIView, attribute: self.secondAttribute,
            multiplier: self.multiplier, constant: self.constant)
        configure(&m)

        // build and return contraint from model
        var constraint = NSLayoutConstraint(
            item: m.firstItem, attribute: m.firstAttribute,
            relatedBy: m.relation,
            toItem: m.secondItem, attribute: m.secondAttribute,
            multiplier: m.multiplier, constant: m.constant)
        return constraint
    }
}
like image 158
Chris Conover Avatar answered Oct 15 '22 07:10

Chris Conover