Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equal height of UICollectionViewCell's and UICollectionView

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)
    }
like image 215
LuckyLuke Avatar asked Dec 16 '15 17:12

LuckyLuke


People also ask

What is the difference between Uitableview and UICollectionView?

Tableiw is a simple list, which displays single-dimensional rows of data. It's smooth because of cell reuse and other magic. 2. UICollectionView is the model for displaying multidimensional data .

What do you mean by a value UICollectionView?

The collection view maintains a queue or list of view objects that the data source has marked for reuse. Instead of creating new views explicitly in your code, you always dequeue views. There are two methods for dequeueing views.


1 Answers

Here is how I would do it. First add a global variable in the view controller which will be used to store the height of the collection view. In the viewDidLayoutSubviews method, set this variable to the height of the collectionView, and if it changed, call a method to invalidate the UICollectionView layout

collectionView.collectionViewLayout.invalidateLayout()

Which will call the method to set sizes of the collectionView cells, and in that method, set the height of the cell to the global variable holding the height of the collectionView.

Note: currently on my mobile and did not test this code. I might have missed something.

like image 189
hannad Avatar answered Oct 13 '22 23:10

hannad