1

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.

Makalele
  • 7,431
  • 5
  • 54
  • 81
  • 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 Jan 03 '18 at 10:18
  • I don't. Description and UiCollectionView have dynamic content - I cannot set the fixed size. – Makalele Jan 03 '18 at 10:19
  • Did you set the stackview's and titlelabel's `translateautoresizingmaskintoconstraints` to false?! In your logs it shows yes – mfaani Jan 04 '18 at 01:52
  • 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 Jan 04 '18 at 07:53
  • hmmm. What is the distribution of stackview? Is it `.fillProportionately`? Can you change it to `fill`? – mfaani Jan 05 '18 at 04:14
  • Is it fill for both. – Makalele Jan 05 '18 at 09:50
  • Just wondering if you ever solved this? I am having a very similar issue and have not found a solution. – coping Dec 08 '19 at 16:38
  • I did, however my layout changed a bit since then. Check this out: https://stackoverflow.com/questions/48093595/making-uitableview-with-embedded-uicollectionview-using-uitableviewautomaticdime – Makalele Dec 09 '19 at 07:12

1 Answers1

0

"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.

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • Because it's pinned to the top of Title label (as I said Title label is pinned to the bottom of the UIStackView). It is working in most cases, but sometimes it breaks apart (on exactly the same content), but after scrolling the bottom and back to the top (so it has to refresh) it's fine again. – Makalele Jan 03 '18 at 13:10
  • I'm not sure I understand you. The stackview's bottom is pinned to the lable's top. What does that have to do with the stackview's top anchor?! – mfaani Jan 03 '18 at 13:19
  • No, label bottom is pinned to the stackview's top. – Makalele Jan 03 '18 at 13:21