简体   繁体   English

如何根据内容调整 UIStackView 的大小?

[英]How to size a UIStackView depending on its content?

I would like to have a similar behavior than the <Table> HTML tag, in the sense where the frame is sized according to its content.在框架根据其内容调整大小的意义上,我希望具有与<Table> HTML 标记类似的行为。

In my very context, I use an UIStackView as the content view of a UITableViewCell.在我的上下文中,我使用 UIStackView 作为 UITableViewCell 的内容视图。 As items in the cell are various information, the resulting height of the cell should be variable.由于单元格中的项目是各种信息,因此单元格的结果高度应该是可变的。

My strategy is to programmatically build a cell as a UIStackView with a .Vertical axis, as in the code snippet as follows:我的策略是以编程方式将单元格构建为带有 .Vertical 轴的 UIStackView,如下代码片段所示:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    let sv = UIStackView(frame: tableview.frame)
    sv.axis = .Vertical
    cell.addSubview(sv)
    for i in information {
        let l = UILabel()
        l.text = i
        sv.addSubViewAndArrange(l)
    }

    return cell
}

Unfortunately, the cell size does not adjust to the content, and as a result I have to set the cell height myself, as follows:不幸的是,单元格大小无法根据内容调整,因​​此我必须自己设置单元格高度,如下所示:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return cellHeight     // a constant
}

How could I fix that?我怎么能解决这个问题?

UIStackView is designed to grow its size according to the content. UIStackView旨在根据内容增加其大小。 In order for that to work, you need to set up the constraints between the UIStackView and the UITableViewCell .为了使其工作,您需要设置UIStackViewUITableViewCell之间的约束。 For example, if UIStackView is first-item and UITableViewCell is it's super-view, then this is how the constraints look like in interface builder:例如,如果UIStackView是第一项,而UITableViewCell是它的超级视图,那么这就是界面生成器中约束的样子:

在此处输入图像描述 在此处输入图像描述

If you like setting up constraints in code, that should work too.如果您喜欢在代码中设置约束,那也应该可以。
For example, assuming stackView and cellView are the names, then above constraints' Swift-code would look like:例如,假设stackViewcellView是名称,那么上述约束的 Swift 代码将如下所示:

stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.topAnchor.constraint(equalTo: cellView.topAnchor, constant: 0).isActive = true
stackView.bottomAnchor.constraint(equalTo: cellView.bottomAnchor, constant: 0).isActive = true
stackView.leadingAnchor.constraint(equalTo: cellView.leadingAnchor, constant: 0).isActive = true
stackView.trailingAnchor.constraint(equalTo: cellView.trailingAnchor, constant: 0).isActive = true

To demonstrate that this will work, I have this for the cellForRowAt function.为了证明这将起作用,我将它用于cellForRowAt函数。 Basically, it puts a number of UILabel inside the UIStackView and the label count is depending on the row number.基本上,它在UIStackView中放置了一些UILabel并且标签计数取决于行号。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableviewCell", for: indexPath) as! TableviewCell
    for i in 1...indexPath.row + 1 {
        let label = UILabel()
        label.text = "Row \(indexPath.row), Label \(i)"
        cell.stackView.addArrangedSubview(label)
    }
    return cell
}

Here is the final result:这是最终结果:

https://github.com/yzhong52/AutosizeStackview https://github.com/yzhong52/AutosizeStackview

I built this example I hope it helps, I've created a tableView which use a cell that contains a stackView and the views loaded in the stackView are gotten from a nib file我构建了这个示例,希望对您有所帮助,我创建了一个 tableView,它使用一个包含 stackView 的单元格,并且在 stackView 中加载的视图是从一个 nib 文件中获取的

https://github.com/Joule87/stackView-within-TableViewCell https://github.com/Joule87/stackView-within-TableViewCell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM