如何自动调整tableView高度+ StackView

发布于 2025-01-19 13:25:31 字数 2153 浏览 2 评论 0原文

我正在尝试实现此功能:

在表格视图的每个单元格中显示标题、描述和图像。 然而,图像可能为零。 要求是:

  1. 当图像为零时,自动扩展标签以适合整个单元格
  2. Tableview 单元格高度应根据描述标签自动调整。
  3. 需要使用 stackView

我以编程方式执行此操作:

  1. 设置这是表视图:

     tableView.rowHeight = UITableView.automaticDimension
     tableView.estimatedRowHeight = 200
    
  2. 创建 2 个 Stackview:一个用于标签,一个用于标签 + 图像:

    let contentStack: UIStackView = {
        var view = UIStackView()
        view.axis = .horizontal
        view.spacing = 16
        view.distribution = .equalSpacing
        view.translatesAutoresizingMaskIntoConstraints  = false
        return view }()
    
    let labelStack: UIStackView = {
        var view = UIStackView()
        view.axis = .vertical
        view.spacing = 5
        view.translatesAutoresizingMaskIntoConstraints = false
        view.distribution = .fillProportionally
        return view }()
  1. 使用自动布局设置约束:
func setupUI() {
        contentView.addSubview(contentStack)
        labelStack.addArrangedSubview(titleLabel)
        labelStack.addArrangedSubview(descriptionLable)
        contentStack.addArrangedSubview(labelStack)
        contentStack.addArrangedSubview(iconView)
        
        NSLayoutConstraint.activate([

            iconView.heightAnchor.constraint(equalToConstant: 80),
            iconView.widthAnchor.constraint(equalToConstant: 80),

            contentStack.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 16),
            contentStack.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
            contentStack.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
            contentStack.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -16)
        ])
        layoutIfNeeded()
    }

但是,此代码不会自动调整 tableview 单元格的高度,您可以从屏幕截图中看到,单元格不会基于关于标签的长度。

使用 stackview 执行此操作的最佳方法是什么?

这是我到目前为止得到的: 输入图片此处描述

I'm trying to implement this feature:

Show title, description and image in each cell of a tableview.
However, the image could be nil.
Requirement is:

  1. When image is nil, auto expand the label to fit the whole cell
  2. Tableview cell height should be adjusted automatically based on the description label.
  3. Need to use stackView

I did this programmatically:

  1. Set this is tableview:

     tableView.rowHeight = UITableView.automaticDimension
     tableView.estimatedRowHeight = 200
    
  2. Create 2 Stackviews: one for labels, one for labels + image:

    let contentStack: UIStackView = {
        var view = UIStackView()
        view.axis = .horizontal
        view.spacing = 16
        view.distribution = .equalSpacing
        view.translatesAutoresizingMaskIntoConstraints  = false
        return view }()
    
    let labelStack: UIStackView = {
        var view = UIStackView()
        view.axis = .vertical
        view.spacing = 5
        view.translatesAutoresizingMaskIntoConstraints = false
        view.distribution = .fillProportionally
        return view }()
  1. Use autolayout to setup the constraints:
func setupUI() {
        contentView.addSubview(contentStack)
        labelStack.addArrangedSubview(titleLabel)
        labelStack.addArrangedSubview(descriptionLable)
        contentStack.addArrangedSubview(labelStack)
        contentStack.addArrangedSubview(iconView)
        
        NSLayoutConstraint.activate([

            iconView.heightAnchor.constraint(equalToConstant: 80),
            iconView.widthAnchor.constraint(equalToConstant: 80),

            contentStack.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 16),
            contentStack.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
            contentStack.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
            contentStack.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -16)
        ])
        layoutIfNeeded()
    }

However, this code doesn't auto resize the height of the tableview cells, you can see from the screenshot, cells won't expand based on the length of the labels.

What's the best way to do this with stackview?

This is what i got so far:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪心 2025-01-26 13:25:31

一些更改...

现在,您的80pt高度图像视图将ContentStack高度限制为80点,因此在您的Content> ContentStack设置中,请使用:

view.alignment = .top

// or

view.alignment = .center

更改您的labelstack设置为:

view.distribution = .fill   // NOT .fillProportionally

确保设置:

titleLabel.setContentCompressionResistancePriority(.required, for: .vertical)
descriptionLabel.setContentCompressionResistancePriority(.required, for: .vertical)

edit

这就是它可以通过长而简短的描述以及/不使用图像来看的方式。标签具有青色背景,图像视图具有黄色背景,因此您可以看到框架...

< img src =“ https://i.sstatic.net/nyats.png” alt =“在此处输入图像说明”>

如果这会留下太多的顶部和底部“填充”,则需要调整顶部,并且底部约束常数。

A few changes...

Right now, your 80-pt Height image view is limiting the contentStack height to 80-points, so in your contentStack setup, use either:

view.alignment = .top

// or

view.alignment = .center

Change your labelStack setup to:

view.distribution = .fill   // NOT .fillProportionally

make sure you set:

titleLabel.setContentCompressionResistancePriority(.required, for: .vertical)
descriptionLabel.setContentCompressionResistancePriority(.required, for: .vertical)

Edit

This is how it can look with long and short descriptions, and with / without an image. The labels have a cyan background and the image views have a yellow background so you can see the frames...

enter image description here

If that is leaving too much top and bottom "padding" you need to adjust your top and bottom constraint constants.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文