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

标签 ios swift uitableview autolayout uistackview

我有两个垂直 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/

相关文章:

ios - iOS 上的 sortedArrayWithSelector 和 NSDictionnary

ios - 无法访问 NSManagedObject 子类的属性(给出 EXC_BAD_ACCESS)

iphone - iOS使用数组添加到UITableView

ios - 删除导航栏和节标题之间的边框线

ios - Swift TableView segue to new ViewController with wrong index path (倒数第二个选择)

iphone - UIImageView 和 UIScrollView 缩放

iphone - 如何以编程方式构建本地化日历月份名称的 NSArray?

ios - 为什么 swift 函数要编译 25 次?

ios - Swift 如何在 UITableView 中为多个部分设置缓存标签值

iphone - iOS变量名和内存管理