更新约束不适用于UITableView snapkit

时间:2017-04-18 01:29:44

标签: ios swift uitableview snapkit

当我尝试在制定约束后立即更新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)
}

1 个答案:

答案 0 :(得分:4)

documentation表示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)
}
相关问题