1
votes

I have an UITableView with a cell which contains:

Title label + UIStackView with some buttons and below it an UIStackView with another label (description), UICollectionView (set of images) and a button. Title label is pinned to leading, trailing and top superview and bottom to the UIStackView (which is pinned to leading, trailing, bottom of label and bottom of the superview).

enter image description here

I'm also using UITableViewAutomaticDimension and that's because content height might be different inside UIStackView (images, text).

Before I've added an UICollectionView to the UIStackView everything worked fine, but after adding it on some occasions layout brokes (everything inside UIStackView is overlapped) and in View UI Hierarchy I can see warnings:

enter image description here

*UIView:0x1038635e0- AMBIGUOUS LAYOUT for UIView:0x1038635e0.Height{id: 3153}

Legend:
    * - is laid out with auto layout
    + - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES
    • - layout engine host
2018-01-03 10:19:58.527094+0100 Crowd Knowledge[1462:1408352] [LayoutConstraints] View has an ambiguous layout. See "Auto Layout Guide: Ambiguous Layouts" for help debugging. Displaying synopsis from invoking -[UIView _autolayoutTrace] to provide additional detail.

*Crowd_Knowledge.AttachmentsCollectionView:0x104072e00- AMBIGUOUS LAYOUT for Crowd_Knowledge.AttachmentsCollectionView:0x104072e00.minY{id: 3263}, Crowd_Knowledge.AttachmentsCollectionView:0x104072e00.Height{id: 3252}

Legend:
    * - is laid out with auto layout
    + - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES
    • - layout engine host
2018-01-03 10:19:58.531696+0100 Crowd Knowledge[1462:1408352] [LayoutConstraints] View has an ambiguous layout. See "Auto Layout Guide: Ambiguous Layouts" for help debugging. Displaying synopsis from invoking -[UIView _autolayoutTrace] to provide additional detail.

*UIButton:0x103854dc0'Edit solution'- AMBIGUOUS LAYOUT for UIButton:0x103854dc0'Edit solution'.minY{id: 3261}, UIButton:0x103854dc0'Edit solution'.Height{id: 3258}

Legend:
    * - is laid out with auto layout
    + - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES
    • - layout engine host
2018-01-03 10:19:59.247887+0100 Crowd Knowledge[1462:1408352] [LayoutConstraints] Window has a view with an ambiguous layout. See "Auto Layout Guide: Ambiguous Layouts" for help debugging. Displaying synopsis from invoking -[UIView _autolayoutTrace] to provide additional detail.

*UIView:0x1038635e0- AMBIGUOUS LAYOUT for UIView:0x1038635e0.Height{id: 3153}

Legend:
    * - is laid out with auto layout
    + - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES
    • - layout engine host

Also, my UICollectionView have a layout, which sets height based on content:

class DynamicCollectionView: UICollectionView {

    override func layoutSubviews() {
        super.layoutSubviews()
        if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
            self.invalidateIntrinsicContentSize()
        }
    }

    override var intrinsicContentSize: CGSize {
        return contentSize
    }

}

I don't have any warnings in storyboard and it works 90% of the time. If it's broken all I have to do is to scroll the other cell so it'll refresh and constraints are working again. I've tried various tricks to force updating the UI (tableView.beginUpdates() + tableView.endUpdates()), but still I cannot find a solution which works always.

I'm looking for a clean solution for this problem.

1
I guess that you don't have a fixed size for StackView - height constraint. Also, please try to add fixed size for all elements in the stackView. - CheshireKat
I don't. Description and UiCollectionView have dynamic content - I cannot set the fixed size. - Makalele
Did you set the stackview's and titlelabel's translateautoresizingmaskintoconstraints to false?! In your logs it shows yes - Honey
I tried it, but still no luck. The problem is when UICollectionView loads list of images and refreshes itself sometimes it breaks the whole UIStackView for some reason. It looks it doesn't know the height. - Makalele
hmmm. What is the distribution of stackview? Is it .fillProportionately? Can you change it to fill? - Honey

1 Answers

0
votes

"UIStackView (which is pinned to leading, trailing and bottom of the superview)"

Why isn't it pinned to the top of the superview? You must pin it to the top of the superview as well. No need for fixed size...

For a very good demo. See this moment of this WWDC video. I highly recommend you see the entire video. But starting from that moment you'll learn a lot about your issue.