Reputation: 113
I have a collection view between two UIViews positioned with auto layout on a screen. When I add cells to this collection view and the horizontal space is full, it creates a new row but this row isn't visible because it doesn't fit on it's parent view height. I want to increase the collection view height when a new row is created so every row is visible. How can I implement this behavior? Is possible to do it with auto layout or I need to do it programmatically?
I created a simple test project to show the problem: https://github.com/eduardoportilho/TestCollectionViewResize
Upvotes: 2
Views: 259
Reputation: 104082
I think you will need to do this in code. Make an IBOutlet to the height constraint you have on the collection view, and the use this code (heightCon is my outlet),
@IBAction func grow(sender: AnyObject) {
itemCount++
coll.reloadData()
coll.layoutIfNeeded()
heightCon.constant = coll.contentSize.height
}
If you want your yellow view to be >= 300 like you have in your constraints, then you should change the priority of the height constraint for the collection view to less than 1000 (I used 750). This will cause your collection view to grow until growing any taller would cause the yellow view to be shorter than 300. Any cells added after that will cause the collection view to scroll instead.
Upvotes: 5