1 year ago

#350613

test-img

Sotiris Kaniras

How to make a horizontal UIStackView grow dynamically in height when content doesn't fit width?

I have a horizontal UIStackView with alignment center and distribution fill, that contains multiple UILabels. My issue is that because there are so many, they are being clipped instead of being placed on a new row.

(user.clubs ?? []).forEach { club in
    let emoji = club["emoji"] as? String ?? ""
    let name = club["name"] as? String ?? ""
    let typeString = club["type"] as? String ?? ""
            
    if let type = ClubType(rawValue: typeString), type == .standard {
       let label = UILabel()
                
       label.text = "\(emoji) \(name)"
       label.textColor = .black
       label.font = UIFont(name: "ArialRoundedMTBold", size: 14)
       label.backgroundColor = .init(white: 0.95, alpha: 1)
       label.layer.cornerRadius = 8
       label.clipsToBounds = true
                
       // ---------------------------
                
       label.translatesAutoresizingMaskIntoConstraints = false
                
       NSLayoutConstraint.activate([
           label.heightAnchor.constraint(equalToConstant: 30)
       ])
                
       // ---------------------------
                
       clubsStackView.addArrangedSubview(label)
   }
}

enter image description here

Any idea what am I doing wrong?

swift

autolayout

uistackview

0 Answers

Your Answer

Accepted video resources