While I put a single UILabel into the stack of UIStackView, everything works fine as expected. But what I need is to actually put a UIView under the UILabel and keep the self-size of UILabel to be kept. I just wondering, is there is a way to achieve this without using UITableView and self-sized cell? I used this code
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)
but it shows the text with only one line which is not making the thing I need.
I have tried the new suggestion with the code
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
I get
when I remove sizeToFit, there is no label at all
My constraints of stackview: