UIStackView 中的 UILabel

UILabel in UIStackView

当我将单个 UILabel 放入 UIStackView 的堆栈中时,一切都按预期正常工作。 但是我需要的是实际上在 UILabel 下放置一个 UIView 并保持 UILabel 的自身大小。 我只是想知道,有没有一种方法可以在不使用 UITableView 和自定大小的单元格的情况下实现这一目标? 我使用了这个代码

let textLabel = UILabel()
    textLabel.text = "zaciatok TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT koniec"
    textLabel.numberOfLines = 0
    textLabel.sizeToFit()
    textLabel.backgroundColor = .yellow
    let testItemView = UIView()
    testItemView.backgroundColor = .green
    testItemView.addSubview(textLabel)
    mainStackView.addArrangedSubview(testItemView)

但它显示的文本只有一行,这不是我需要的东西。

我已经用代码尝试了新的建议

mainStackView.distribution = .fill
    let textLabel = UILabel()
    textLabel.text = "zaciatok TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT koniec"
    textLabel.numberOfLines = 0
    textLabel.sizeToFit()
    textLabel.backgroundColor = .yellow
    let testItemView = UIView()

    testItemView.backgroundColor = .green
    testItemView.addSubview(textLabel)

    mainStackView.addArrangedSubview(testItemView)
    testItemView.translatesAutoresizingMaskIntoConstraints = false
    testItemView.heightAnchor.constraint(equalToConstant: 50).isActive = true

我明白了

当我删除 sizeToFit 时,根本没有标签 我对 stackview 的限制:

1- 对于要换行的标签内的文本,必须使用前导和尾随约束正确挂钩 mainStackView(当然还要使用顶部和底部以自动调整大小)

2- 您需要删除

textLabel.sizeToFit()

3- 您添加的视图没有像标签那样的固有内容大小,因此您需要给它一个静态高度约束

let testItemView = UIView()
testItemView.translatesAutoresizingMaskIntoConstraints = false
testItemView.heightAnchor.constraint(equalToConstant: 50).isActive = true

4-设置

mainStackView.distribution = .fill

试试这个

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white

        let mainStackView = UIStackView()
        mainStackView.axis = .vertical
        mainStackView.spacing = 3
        mainStackView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(mainStackView)

        mainStackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(10)-[mainStackView]-(10)-|", options: [], metrics: nil, views: ["mainStackView":mainStackView]))

        let testItemView = UIView()
        testItemView.layer.maskedCorners = [.layerMinXMinYCorner,.layerMinXMaxYCorner,.layerMaxXMinYCorner]
        testItemView.layer.cornerRadius = 15.0
        testItemView.layer.masksToBounds = true
        testItemView.backgroundColor = .green
        testItemView.translatesAutoresizingMaskIntoConstraints = false

        let textLabel = UILabel()
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        textLabel.text = "zaciatok TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT TEXT TEXTTEXT TEXT koniec"
        textLabel.numberOfLines = 0
        textLabel.lineBreakMode = .byWordWrapping
        textLabel.backgroundColor = .yellow
        testItemView.addSubview(textLabel)

        testItemView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(5)-[textLabel(>=30)]-(5)-|", options: [], metrics: nil, views: ["textLabel":textLabel]))
        testItemView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(5)-[textLabel]-(5)-|", options: [], metrics: nil, views: ["textLabel":textLabel]))

        mainStackView.addArrangedSubview(testItemView)
    }
}