Equal height of UICollectionViewCell and UICollectionView

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) } 
+15
source share
6 answers

This is how I do it. First, add a global variable to the view controller that will be used to store the height of the collection view. In the viewDidLayoutSubviews method viewDidLayoutSubviews set this variable to the height of the viewDidLayoutSubviews collection, and if it changes, call the method to invalidate the UICollectionView layout

 collectionView.collectionViewLayout.invalidateLayout() 

Which will call the method to set the sizes of the collectionView cells, and in this method set the cell height of the global variable containing the collection height.

Note : currently on my mobile phone and do not check this code. Maybe I missed something.

+11
source

Try setting this code to viewDidLoad :

 self.automaticallyAdjustsScrollViewInsets = false 

Hope this help!

+10
source

I ran into this problem and I was able to solve it by reading the corresponding sectionInsets . When you subtract inserts that are not taken into account in the .size.height property, it takes height.

Inside sizeForItemAtIndexPath you need to subtract section insertion sizes ...

  let sectionInset = self.collectionView?.collectionViewLayout.sectionInset let heightToSubtract = sectionInset!.top + sectionInset!.bottom return CGSize(width: 100.0, height: (self.collectionView?.bounds.height)! - heightToSubtract) 

NOTE. I can disable the options here, but to be safe, you can check them out.

0
source

I had this problem because I attached my UICollectionView to the bottom of the UITableViewCell without specifying the height of the collectionView .

After creating several network requests and then calling [self.tableView reloadData] in the UIViewController , I received the same error message in the log.

I managed to resolve it by breaking the lower bound and specifying a UICollectionView fixed height.

0
source

In my case, I add the view collection to the tableviewcell view, and the height of the collectionview element is equal to the height of the tableviewcell. when changing tableviewcellheiht I get this warning. to solve this problem try this code

  if (@available(iOS 11.0, *)) { _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { } 
0
source

I struggled with this problem for more than a week and finally found an answer that worked for my specific case. My problem was 1) loading deleted images in a UICollectionViewCell image, and 2) an estimated cell size that was larger than the one I manually set for sizeForItemAt of the collection view layout.

enter image description here

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height) } 

I was able to solve by reducing the size of the cell on the storyboard to a size smaller than the size of my cell, and I also set the "Estimate Size" to "None" (this last part is probably enough)

0
source

Source: https://habr.com/ru/post/1238411/


All Articles