SnapKit Constraint Binding Update

I am using SnapKit and cannot find a clean way to update the offset constraint. This is a simple view of the loading panel, whose internal view (expiredTime) should fill the parent (from left to right) in accordance with the percentage.

I create a constraint in a awakeFromNibcustom UIView

    self.expiredTime.snp.makeConstraints { (make) in
        make.left.equalTo(self)
        make.top.equalTo(self)
        make.bottom.equalTo(self)
        self.constraintWidth = make.width.equalTo(self).constraint 
    }
    setNeedsUpdateConstraints()

then whenever the time is updated, I call setNeedsUpdateConstraints(), which calls by default updateConstraints (), which was overloaded, like an apple hint:

Doesn't work: (expiredTime view always matches parent view)

override func updateConstraints() {

    let offset = self.frame.width * CGFloat(percentage)

    self.expiredTime.snp.updateConstraints { (make) in
        make.width.equalTo(self).offset(offset).constraint
    }
    super.updateConstraints()
}

This also does not work :

override func updateConstraints() {
    let offset = self.frame.width * CGFloat(percentage)
    self.constraintWidth?.update(offset: offset)
    super.updateConstraints()
}

Restoring all restrictions works , but I would like to avoid it

override func updateConstraints() {

    self.expiredTime.snp.remakeConstraints() { (make) in
        make.left.equalTo(self)
        make.top.equalTo(self)
        make.bottom.equalTo(self)
        self.constraintWidth = make.width.equalTo(self).multipliedBy(self.percentage).constraint
    }
    super.updateConstraints()
}
+4
1

, width expiredTime- . , 0, . , :

override func updateConstraints() {
    let width = self.frame.width * CGFloat(percentage)
    self.expiredTime.snp.updateConstraints { (make) in
        make.width.equalTo(width)
    }
    super.updateConstraints()
}

, , updateConstraints() . update ( setNeedsUpdateConstraints() )

constraintWidth?.update(offset: CGFloat(percentage) * view.bounds.width)

0 constraintWidth:

self.expiredTime.snp.makeConstraints { (make) in
    make.left.top.bottom.equalTo(self)
    self.constraintWidth = make.width.equalTo(0).constraint 
}
+6

Source: https://habr.com/ru/post/1668717/


All Articles