'Setting ScrollView Height issue when UICollectionView is nested

I Need to adjust a child collection view height when I've calculated what it's parent scrollView should be. The height calculation produces the correct value but I can't get the "Grand child" collectionView to adjust itself.

All the constraints inside the storyBoard are tied to the scrollView container view height so when I adjust it's value they all should follow in line and adjust themselves.

The flow is:

UIScrollView -> UICollectionView -> UICollectionView

My calculation is:

    var count: CGFloat = 370

    guard let currentLegs = active.legs![cell].feet else { return }

    for _ in currentLegs {
        count += 70
    }

    UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {

        if count > 540 {
            self.scrollView.contentSize.height = count
        } else {
            self.scrollView.contentSize.height = 540
        }

    }, completion: nil)

    print(count)

The above calculates the height and defaults to another value if it's smaller than I need.

I've tried setting the scrollview.layoutIfneeded() & setNeedsLayout() but these cause the collectionView to redraw itself resulting in a crash.

Is it possible to adjust a "grand child" collection view from the top level?

Thanks



Solution 1:[1]

Try this

 @IBOutlet weak var scrollViewHeightConstarint: NSLayoutConstraint!

.....

override func viewDidLayoutSubviews() {
   super.viewDidLayoutSubviews()
   self.scrollViewHeightConstarint.constant = <#WhatYouNeed#> 
   self.view.layoutIfNeeded() 
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1