How do I set the cell height in a UICollectionView equal to the height of any kind of collection? The code below does not work, because the height of the collection views is not known at the moment, it seems, since the automatic layout is tinkering with properties at this stage. This leads to the fact that the height of the cells is higher than the actual appearance of the collection.
I will add 300 as a reward for the answer that solves this!
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return CGSize(width: 100, height: collectionView.frame.size.height) }
2015-12-16 18: 43: 53.643 My-app [1055: 434762] code entry behavior hereUICollectionViewFlowLayout is not defined because: 2015-12-16 18: 43: 53.643 My-app [1055: 434762] element height must be less than the height of the UICollectionView minus sectional inserts at the top and bottom of the value, minus the top and bottom values ββof the content insert. 2015-12-16 18: 43: 53.643 My-app [1055: 434762] Please check the values ββreturned by delegate. 2015-12-16 18: 43: 53.644 My-app [1055: 434762] The corresponding Instance is UICollectionViewFlowLayout, and it is bound to; layer =; contentOffset: {0, 0}; contentSize: {3087, 307}> collection layout :. 2015-12-16 18: 43: 53.644 My-app [1055: 434762] Make a symbolic breakpoint in UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
A solution that works based on the hannads offer. If there are better ways, please let me know.
Created a property with a property observer.
var myCollectionViewHeight: CGFloat = 0.0 { didSet { if myCollectionViewHeight != oldValue { myCollectionView.collectionViewLayout.invalidateLayout() myCollectionView.collectionViewLayout.prepareLayout() } } }
Override this method (it is called several times)
override func viewDidLayoutSubviews() { myCollectionViewHeight = myCollectionView.bounds.size.height }
Then I have this in my delegate:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return CGSize(width: 100, height: myCollectionViewHeight) }