1

I have the following ViewController, with 3 UIViews and content inside. The red lines are my constraints.

enter image description here

The black one has always a fixed height.

The red UIView has a CollectionView inside, which will grow, depending on it's items.

The green is a UITableView which should shrink, depending on the size of the red UICollectionView.

I tried to set the height constraint of the red (UICollectionView) to greater than or equal, and the green (UITableView) height to less than or equal, but Swift is telling me to set a x or height value.

What is the correct approach to get two dynamic Views inside my ViewController?

ProjektWeinheim
  • 201
  • 3
  • 10

1 Answers1

0

Create NSLayoutConstraint for CollectionView Height,Then set the height on cellForItemAt function

var collHeight:NSLayoutConstraint? //In class scope

Assign CollectionView height anchor to collHeight

collHeight = collectionView.heightAnchor.constraint(equalToConstant: 0)
collHeight!.isActive = true

Then set the height on cellForItemAt function

   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        collHeight.constant = collectionView.contentSize.height
    }

it will expand the size of collectionview height according to item count. Next set the tableview top anchor to collectionview bottom anchor

Dilan
  • 2,610
  • 7
  • 23
  • 33