imzyf / ios-swift-learning-notes

📝 iOS Swift Learning Notes - see Issues
MIT License
0 stars 0 forks source link

snapkit 更新约束 #77

Open imzyf opened 6 years ago

imzyf commented 6 years ago

http://www.hangge.com/blog/cache/detail_1110.html

删除约束

var sizeConstraint:Constraint?

...

    box.snp.makeConstraints { (make) -> Void in
            self.sizeConstraint = make.width.height.equalTo(150).constraint
            make.center.equalTo(self.view)
     }

...

self.sizeConstraint?.deactivate()
imzyf commented 6 years ago

通过约束的引用来更新约束

self.topConstraint?.update(offset: 60)
imzyf commented 6 years ago

使用snp.updateConstraints更新约束

override func updateViewConstraints() {
        self.box.snp.updateConstraints{ (make) -> Void in
            //视图宽度与屏幕等宽
            make.width.equalTo(self.view)
        }
         
        super.updateViewConstraints()
    }

我们还可以用 snp.updateConstraints 方法来代替 snp.makeConstraints 进行约束的更新,这个更新操作通常放在 UIViewController 的 updateViewConstraints() 方法中,或者 UIView 的 updateConstraints() 方法中执行,这样视图约束需要更新的时候会自动调用。

imzyf commented 6 years ago

使用snp.remakeConstraints重做约束

snp.remakeConstraints 与 snp.makeConstraints 用法类似,不同的是 snp.remakeConstraints 首先会先清除掉之前所有被SnapKit设置的约束。

box.snp.remakeConstraints { (make) -> Void in
            make.width.height.equalTo(100)
        }