我有嵌套在水平StackView
两个垂直StackView
。
对于第一种:
titlesView.axis = .vertical
titlesView.distribution = .fill
titlesView.alignment = .leading
而第二个:
checkView.axis = .vertical
checkView.distribution = .fil
checkView.alignment = .center
和用于holderStackView(根)
holderStackView.axis = .horizontal
holderStackView.alignment = .center
holderStackView.distribution = .fill
holderStackView.addArrangedSubview(titlesView)
holderStackView.addArrangedSubview(checkView)
titlesView.setContentHuggingPriority(.defaultLow, for: .horizontal)
checkView.setContentHuggingPriority(.defaultHigh, for: .horizontal)
我预计titlesView必须填写StackView,但checkView StackView填充holderStackView堆栈视图。
如果我对他们俩的center
或leading
设置对齐,那么它工作正常,第一个增长。
期望:
现实:
我怎样才能解决这个问题呢?
它只会发生在contentView
的UITableViewCell
,请参见下面的小例子:
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorColor = UIColor(red:0.87, green:0.87, blue:0.87, alpha:1.00)
tableView.estimatedRowHeight = 150
tableView.backgroundColor = UIColor.clear
tableView.rowHeight = UITableViewAutomaticDimension
self.view.backgroundColor = UIColor(red:0.93, green:0.93, blue:0.93, alpha:1.00)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return MedicationHomeCell()
}
}
class MedicationHomeCell: UITableViewCell {
let holderStackView = UIStackView()
let checkView = UIStackView()
let titlesView = UIStackView()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
layoutViews()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
layoutViews()
}
func layoutViews() {
self.contentView.backgroundColor = .white
self.contentView.addSubview(holderStackView)
holderStackView.addArrangedSubview(titlesView)
holderStackView.addArrangedSubview(checkView)
titlesView.axis = .vertical
titlesView.distribution = .fill
titlesView.alignment = .leading
checkView.axis = .vertical
checkView.distribution = .fill
checkView.alignment = .center
holderStackView.axis = .horizontal
holderStackView.alignment = .center
holderStackView.distribution = .fill
titlesView.setContentHuggingPriority(.defaultLow, for: .horizontal)
checkView.setContentHuggingPriority(.defaultHigh, for: .horizontal)
holderStackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
holderStackView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor),
holderStackView.rightAnchor.constraint(equalTo: self.contentView.rightAnchor),
holderStackView.topAnchor.constraint(equalTo: self.contentView.topAnchor),
holderStackView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor)
])
for index in 0..<2 {
let label = UILabel()
label.text = "Label \(index)"
label.backgroundColor = .red
titlesView.addArrangedSubview(label)
}
for index in 0..<2 {
let label = UILabel()
label.text = "Text \(index)"
label.backgroundColor = .green
checkView.addArrangedSubview(label)
}
}
}
UIStackView的似乎不尊重内容拥抱和抗压缩性。取而代之的是,将设置在每安排子视图这些属性。
对于嵌套组的意见,你可能需要深入到他们的非堆叠排列子视图,并设置属性那里。