我已以编程方式在设置为 IBOutlet 的 UISwitch 上设置了以下约束:
func setConstraints {
let leadingConstraintTimeSwitch1 = NSLayoutConstraint(item: timeSwitch, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: timeLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 10)
view.addConstraint(leadingConstraintTimeSwitch1)
每当我更改此约束的常数时,开关的 X 位置保持不变。但是,当我转到开关的尺寸检查器并更改其 X 值时,开关的位置确实正确更新,但我不想使用尺寸检查器。如何以编程方式获取约束集以覆盖尺寸检查器中的 X 值?我还添加了一个约束,指定开关的 Y 位置(底部约束),并且这个确实起作用。谢谢!
最佳答案
您的描述中没有太多信息可供引用。
我通常像这样添加约束:
NSLayoutConstraint(item: timeSwitch, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: timeLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 10).active = true
timeSwitch
和 timeLabel
是 View 层次结构的一部分吗?
您是否将 translatesAutoresizingMaskIntoConstraints
设置为 false?
还有其他可能影响它的限制吗?
关于ios - X 位置约束未以编程方式更新 - Xcode 中的 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37240785/