[英]UIStackView: add arranged views with the same width
我有 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")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.