我有UIStackView并希望在此视图中添加一些排列的视图...有时一个,有时两个,三个......等等.UIStackView具有全宽。我希望排列的视图具有左对齐的相同宽度,以便它们不会填充UIStackView的整个宽度。
这就是我所拥有的:
这就是我想要的:
是否有可能以某种方式进行,或者我是否需要更改UIStackVIew的宽度,具体取决于我添加的排列数量?
我的代码:
// creating views
stackView = {
let v = UIStackView()
v.axis = .horizontal
v.alignment = .center
v.distribution = .fillEqually
v.spacing = 5
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
if let img = chosenImage {
let imageView: UIImageView = {
let l = UIImageView()
l.translatesAutoresizingMaskIntoConstraints = false
l.image = img
return l
}()
self.stackView?.addArrangedSubview(imageView)
imageView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal, toItem: imageView, attribute: .height, multiplier: 1, constant: 1))
imageView.setNeedsUpdateConstraints()
} else {
print("Something went wrong")
}