dkoch
dkoch

Reputation: 474

UIStackView adjust height to subviews

I want the UIStackView to resize to fit their subviews (UIImageView and UILabel in this case)

    let headerView = UIStackView()
    headerView.axis = .vertical
    headerView.alignment  = .center
    headerView.distribution = .equalSpacing
    headerView.spacing = 10

    let headerImage = UIImageView(...)
    headerImage.contentMode = .scaleAspectFill
    headerImage.clipsToBounds = true
    headerImage.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: tableView.frame.width / 1.618)

    let desciptionView = UILabel()
    desciptionView.text = "Some very long text wrapping multiple lines..."
    desciptionView.numberOfLines = 0
    desciptionView.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)

    headerView.addArrangedSubview(headerImage)
    headerView.addArrangedSubview(desciptionView)

    print(headerView.bounds) // always 0,0,0,0
    print(headerView.frame) // always 0,0,0,0

    tableView.tableHeaderView = headerView

(in this code height and width are 0)

How to implement the wanted behaviour?

Upvotes: 4

Views: 10630

Answers (1)

Chris Conover
Chris Conover

Reputation: 9039

The only way I have found to do this is by setting the content hugging and compression resistance priorities of the child views to be required, something like this:

    let arrangedViews = [filterLabel, image]
        .map { (view: UIView) -> UIView in
            view.setContentHuggingPriority(.required, for: .horizontal)
            view.setContentCompressionResistancePriority(.required, for: .horizontal)
            return view }

    let stack = UIStackView(arrangedSubviews: arrangedViews)
    stack.axis = .horizontal
    stack.distribution = .fill
    stack.spacing = 8

With those set on the arranged subviews, do you do indeed (or at least I did) get the desired result.

Upvotes: 8

Related Questions