【发布时间】:2016-11-17 16:01:37
【问题描述】:
在代码中,我添加了 4 个约束。但是当我在 UI 调试器中运行应用程序时,它会显示额外的 6 个约束?我添加的这4个应该很清楚。我不知道我做错了什么?
let leftConstr = NSLayoutConstraint(item: image, attribute: .leading, relatedBy: .equal, toItem: cell.contentView, attribute: .leading, multiplier: 1.0, constant: 0.0)
let bottomConstr = NSLayoutConstraint(item: image, attribute: .bottom, relatedBy: .equal, toItem: cell.contentView , attribute: .bottom, multiplier: 1.0, constant: 0.0)
let highthConstr = NSLayoutConstraint(item: image, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 15)
let widthConstr = NSLayoutConstraint(item: image, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50)
cell.contentView.addConstraints([highthConstr, widthConstr, bottomConstr, leftConstr])
这是我的 UI 调试器的屏幕截图
【问题讨论】:
-
我也看到了这种情况。如果我尝试以编程方式设置约束,它似乎会自动生成我没有创建的约束以使场景正常工作。也许“translatesAutoresizingMaskIntoConstraints”就是这样做的。
标签: ios swift autolayout nslayoutconstraint