gpt4 book ai didi

ios - UIStackView 内容拥抱在 UITableViewCell 中没有按预期工作

转载 作者:搜寻专家 更新时间:2023-10-31 19:36:26 25 4
gpt4 key购买 nike

我有两个垂直 StackView 嵌套在水平 StackView 中。
对于第一个:

titlesView.axis = .vertical
titlesView.distribution = .fill
titlesView.alignment = .leading

第二个:

checkView.axis = .vertical
checkView.distribution = .fil
checkView.alignment = .center

和 holderStackView(root)

    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 堆栈 View 。

如果我为它们 centerleading 设置对齐方式,那么它会按预期工作并且第一个会增长。



期望:

enter image description here



现实:

enter image description here



我该如何解决这个问题?

它只发生在 UITableViewCellcontentView 中,参见下面的最小示例:

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)
}
}
}

最佳答案

UIStackViews 似乎不尊重内容拥抱和阻力压缩属性。相反,在每个排列的 subview 上设置这些属性。

对于嵌套的堆栈 View ,您可能需要深入到其非堆栈排列的 subview ,并在那里设置属性。

关于ios - UIStackView 内容拥抱在 UITableViewCell 中没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47739499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com