2017-04-18 43 views
0

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

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) 
} 

回答

2

documentation说updateConstraints仅用于更新现有的约束常数。

的选择,如果你只更新 约束的恒定值,你可以用它代替 snp.makeConstraints

你没有更新的恒方法snp.updateConstraints;您正试图将约束分配给新的锚。

,我认为你应该做的是采取参考顶部约束:

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) 
} 
+0

你是什么意思只是常数?我在'UIView.animate'中使用updateConstraints,所以我需要将它保留为updateConstraints。 – SmedleyDSlap

+0

NSLayoutConstraint的.constant属性。我不明白你为什么不能为UIView.animate使用makeConstraints;虽然我不清楚如果调用makeConstraints调用layoutIfNeeded。如果不是,则先调用约束条件,然后将self.view.layoutIfNeeded放入动画块中以触发自动布局重新计算和框架更改。 –