UICollectionView Cell Auto Size with AutoLayout iOS 8

I am trying to configure iOS 8 to automatically calibrate cells in a UICollectionViewController :
I created a basic project using StoryBoards and developed one UICollectionViewCell there with a label limited to cell restrictions. Then I associated this cell label with the Cell class to access it programmatically.
Without specifying estimatedItemSize everything works fine (except for auto-size, of course).

When I specify estimatedItemSize as follows:

 - (void)viewDidLoad { [super viewDidLoad]; UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout; layout.estimatedItemSize = CGSizeMake(100.0f, 30.0f); } 

Applications crash with these error messages:

 the behavior of the UICollectionViewFlowLayout is not defined because: the item width must be less than the width of the UICollectionView minus the section insets left and right values. Please check the values return by the delegate. 

I think I'm setting estimatedItemSize in the wrong place, or if I already have a specific item size configured in the storyboard, so that it somehow conflicts with the estimated size.

+6
source share
1 answer

Answering my own question, as I found out the cause of the problem:
For this function to work, the UILabel must be less than the width of the UICollectionView. Even if it looks fine on the Storyboard, if AutoLayout is used, the width should be limited, for example. in my case, I made a restriction of less or equal 320 . Without it, the label grows to a very large width, but with only one line. When this restriction was applied, the wrapping of the lines was completed, and the label increased only in height, and not just in width.

+4
source

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


All Articles