【发布时间】:2015-06-09 15:12:33
【问题描述】:
我正在尝试水平排列 5 个视图,目前我的视觉格式和视图属性(省略颜色)如下所示:
let questionLabel = UILabel()
questionLabel.text = "Did you take any additional medication?"
questionLabel.lineBreakMode = .ByWordWrapping
questionLabel.numberOfLines = 0
let questionSwitch = UISwitch()
let noLabel = UILabel()
noLabel.text = "No"
let yesLabel = UILabel()
yesLabel.text = "Yes"
for v in [questionLabel, questionSwitch, noLabel, yesLabel] {
scrollView.addSubview(v)
v.setTranslatesAutoresizingMaskIntoConstraints(false)
}
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-20-[label]-(>=10,==10@900)-[no(==25)]-5-[switch]-5-[yes(==30)]-10-|",
options: .AlignAllCenterY, metrics: nil,
views: ["label":questionLabel, "switch":questionSwitch, "no":noLabel, "yes":yesLabel]))
我已经硬编码了“否”和“是”标签的宽度,因为它有时会试图使它们变大,这会导致出现更大的边距。我希望有一个灵活大小的唯一边距是在问题和“否”标签之间。
当屏幕的宽度大到足以将label 的文本放在一行时,这很有效,但是当它必须进行换行时,会以一种不受欢迎的方式转移内容:
它确实将标签分成 2 行,但随后在开关顶部挤压“是”。调试器中没有关于破坏约束的警告。我错过了什么?
【问题讨论】:
标签: ios swift autolayout