【发布时间】:2019-02-08 01:50:43
【问题描述】:
我将UILabel 放入UIScrollView 中,我想将这些约束添加到标签中:
上边距为 20
左边距为 20
右边距为 20
标签高度为 40
这是我使用视觉格式为这些约束编写的代码:
let label = UILabel.init(frame: .zero)
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Some Text"
scrollView.addSubview(label)
let horizontalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-20-[label]-20-|",
options: [],
metrics: nil,
views: ["label": label]
)
let verticalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "V:|-20-[label(40)]",
options: [],
metrics: nil,
views: ["label": label]
)
NSLayoutConstraint.activate(horizontalConstraints + verticalConstraints)
但是this picture 是我得到的(无法上传照片,因为我的声誉很低)。我给视图添加了边框,蓝色的是UIScrollView的边框,红色的是UILabel的边框
【问题讨论】:
-
使用
NSLayoutConstraint会好很多 -
scrollView的约束是否已正确设置? -
另外,需要处理FVL吗?我会假设有更简单的选择,你可能想检查一下:stackoverflow.com/questions/26180822/…
-
谢谢你们,我已经在下面发布了答案