1

当我在创建约束后立即尝试更新 snapkit 约束时,出现Updated constraint could not find existing matching constraint to update错误。我正在使用的代码如下:

let directionList = UITableView()
directionList.delegate = self
directionList.dataSource = self
directionList.tag = DIRECTIONS_TABLE_TAG
self.view.addSubview(directionList)
directionList.snp.makeConstraints{ (make) -> Void in
    make.width.equalToSuperview()
    make.top.equalTo(self.view.snp.bottom)
    make.left.equalToSuperview()
    make.right.equalToSuperview()
    make.bottom.equalToSuperview().offset(-20)
}

directionList.snp.updateConstraints { (make) -> Void in
   make.top.equalTo(self.topDarkBlue.snp.bottom)
}
4

2 回答 2

5

文档说 updateConstraints 仅用于更新现有约束的常量 。

或者,如果您只更新约束的常量值,您可以使用方法 snp.updateConstraints 而不是 snp.makeConstraints

您没有更新常量;您正在尝试将约束分配给新的锚点。

我认为你应该做的是参考顶级约束:

var topConstraint: Constraint? = nil
...
topConstraint = make.top.equalTo(self.view.snp.bottom)

稍后删除顶部约束:

topConstraint.uninstall()

然后使用另一个块来制作新的约束。

directionList.snp.makeConstraints{ (make) -> Void in
   make.top.equalTo(self.topDarkBlue.snp.bottom)
}
于 2017-04-20T07:01:37.537 回答
0

我迟到了,但可能对某人有帮助,最佳答案有一个错误

换线

topConstraint = make.top.equalTo(self.view.snp.bottom)

topConstraint = make.top.equalTo(self.view.snp.bottom).constraints

还有一件事情

topConstraint.deactivate()

因为卸载已弃用。

于 2022-02-21T20:16:42.070 回答