ios - X 位置约束未以编程方式更新 - Xcode 中的 Swift

标签 ios xcode swift constraints nslayoutconstraint

我已以编程方式在设置为 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

timeSwitchtimeLabel 是 View 层次结构的一部分吗?

您是否将 translatesAutoresizingMaskIntoConstraints 设置为 false?

还有其他可能影响它的限制吗?

关于ios - X 位置约束未以编程方式更新 - Xcode 中的 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37240785/

相关文章:

ios - 如何使 UIImage 适合整个 UINavigationBar View 的大小

android - 在考虑电池生命周期的情况下开发Xcode或Android SDK是否优于Unity3d?

objective-c - Xcode,如何更改一个目标的图像文件?

swift - Swift Switch 会短路吗?

ios - 边框是圆角裁剪的吗? swift

ios - 打造iOS Gallery - UICollectionView的可重用性

ios - 无法摆脱致命异常 : NSInternalInconsistencyException Error

ios - Swift iOS 声音内存泄漏?

ios - 通过数据库搜索值

ios - 如何在 Swift 中设置 UIScrollView 高度