以编程方式添加的约束并不总是有效

时间:2018-05-14 16:04:16

标签: ios swift tableview constraints indentation

我正在以编程方式在单元格上添加这些约束,以便缩进子选项卡。然而,大部分时间它都有效,如截图所示,它并不总是有效。下面的代码显示了约束。 enter image description here

let views: [String:Any] = ["parent": self, "view": self.borderView, "subview": self.backView, "button1": self.actionButton, "label": self.name, "button2": self.nextButton]
    let margin1 = NSLayoutConstraint.constraints(withVisualFormat: "|-\(margin*level+8)-[subview]-8-|",
        options: .alignAllCenterY,
        metrics: nil,
        views: views)

    let margin2 = NSLayoutConstraint.constraints(withVisualFormat: "|-\(margin*level+8)-[view]-8-|",
        options: .alignAllCenterY,
        metrics: nil,
        views: views)

    let margin3 = NSLayoutConstraint.constraints(withVisualFormat: "|-8-[button1]-[label]-[button2]-8-|",
                                                 options: .alignAllCenterY,
                                                 metrics: nil,
                                                 views: views)

    self.addConstraints(margin1)
    self.addConstraints(margin2)
    self.addConstraints(margin3)

    self.setNeedsUpdateConstraints()
    self.updateConstraintsIfNeeded()
    self.updateConstraints()

我正在使用autolayout,这些单元格位于自己的xib文件中。任何人都有线索?

0 个答案:

没有答案