Quantcast
Channel: User LuckyLuke - Stack Overflow
Viewing all articles
Browse latest Browse all 37

Equal height of UICollectionViewCell's and UICollectionView

$
0
0

How am I supposed to set the height of the cells in UICollectionView equal to the height of whatever the collection view is? The code below doesn't work because the collection views height is not known at this point it seems since the auto layout is messing with the properties at this stage. It causes the cells height to be higher than the actual collection view.

I will add 300 in bounty to an 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] the behavior of the enter code hereUICollectionViewFlowLayout is not defined because: 2015-12-16 18:43:53.643 My-app[1055:434762] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values. 2015-12-16 18:43:53.643 My-app[1055:434762] Please check the values return by the delegate. 2015-12-16 18:43:53.644 My-app[1055:434762] The relevant UICollectionViewFlowLayout instance is , and it is attached to ; layer = ; contentOffset: {0, 0}; contentSize: {3087, 307}> collection view layout: . 2015-12-16 18:43:53.644 My-app[1055:434762] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

Solution that works based on hannads suggestion. If there are better ways please let me know

Made 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 multiple 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)    }

Viewing all articles
Browse latest Browse all 37

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>