【问题标题】:UIStackView content hugging is not working as expected in UITableViewCellUIStackView 内容拥抱在 UITableViewCell 中没有按预期工作
【发布时间】:2018-05-24 04:24:13
【问题描述】:

我有两个垂直的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 堆栈视图。

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



期望:



现实:



我该如何解决这个问题?

它只发生在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)
        }
    }
}

【问题讨论】:

  • 您是否设置了正确的内容拥抱(水平)? +我更喜欢看代码
  • @MilanNosáľ,我编辑并添加了一些代码
  • 我已更新您的代码,以便其他人更轻松地试用它。我遇到了同样的问题,到目前为止,我不知道为什么会这样。
  • 你找到答案了吗?我有同样的问题,我无法解释
  • 很遗憾,没有。

标签: ios swift uitableview autolayout uistackview


【解决方案1】:

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

对于嵌套堆栈视图,您可能需要深入到它们的非堆栈排列的子视图,并在那里设置属性。

【讨论】:

    猜你喜欢
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 2013-04-20
    • 2018-12-01
    相关资源
    最近更新 更多