【问题标题】:Xcode adds duplicate constraints?Xcode 添加重复约束?
【发布时间】: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


【解决方案1】:

也许你可以尝试在之前添加这行代码:

image.translatesAutoresizingMaskIntoConstraints = false

【讨论】:

  • 这就是解决方案!谢谢@Matthias
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-21
  • 2021-01-19
  • 2018-01-21
  • 2019-02-26
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多