Uicollectionviewcell Not Forcing Cell Size

UICollectionViewCell not forcing Cell Size

The problem is that in the storyboard your collection view's Estimate Size is configured to Automatic:

Sample Image

You need to set it to None if you want your sizeForItemAt implementation to be obeyed rather than the internal constraints.

UICollectionViewCell content wrong size on first load

It looks like it still retains it's 100x100px basic size for the contentView while the cell itself gets the right size already. You can force the layout to reset adding the following line just before you return the cell:

cell?.layoutIfNeeded()

Adding UILabel forces UICollectionViewCell to shrink width

You need to change horizontal contentHuggingpriority of the label to low most value so it can stretch freely

Resize UICollectionView cells after their data has been set

I think your are looking for the invalidateLayout method you can call on the .collectionViewLayout property of your UICollectionView. This method regenerates your layout, which in your case means also calling -collectionView: layout: sizeForItemAtIndexPath:, which is the right place to reflect your desired item size. Jirune points the right direction on how to calculate them.

An example for the usage of invalidateLayout can be found here. Also consult the UICollectionViewLayout documentation on that method:

Invalidates the current layout and triggers a layout update.

Discussion:

You can call this method at any time to update the layout information. This method invalidates the layout of the collection view itself and returns right away. Thus, you can call this method multiple times from the same block of code without triggering multiple layout updates. The actual layout update occurs during the next view layout update cycle.

Edit:

For storyboard collection view which contains auto layout constraints, you need to override viewDidLayoutSubviews method of UIViewController and call invalidateLayout collection view layout in this method.

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[yourCollectionView.collectionViewLayout invalidateLayout];
}

Custom height of UICollectionViewCell ignored

There is an Estimate Size property of UICollectionView which is restricting custom height to be set. You can update its value from storyboard. Select UICollectionView -> Size Inspector -> Estimate Size and set its value to None. That way your custom height will work like charm!

How to configure a UICollectionView Cell Size per Size Class?

The solution I came up with was just to implement the UICollectionViewDelegateFlowLayout and implement the sizeForItemAtIndexPath method.
This means that the cell dimensions can be set to match the available Size Class dimensions.

This still isn't ideal as you can't see the changes in the storyboard and you can't create a universal design and see it in each of the different formats.

I'm still hoping someone has a better option.

Prevent sizeForItemAt from using the wrong cell size because of reuse for dynamic collectionView cell

You have the right idea, but when you measure the cell by its internal constraints by calling systemLayoutSizeFitting, instead of calling systemLayoutSizeFitting on an existing cell (collectionView.cellForItem), you need to arm yourself with a model cell that you configure the same as cellForItem would configure it and measure that.

Here's how I do it (remarkably similar to what you have, with that one difference; also, I store the size in the model):

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let memosize = self.sections[indexPath.section].itemData[indexPath.row].size
if memosize != .zero {
return memosize
}
self.configure(self.modelCell, forIndexPath:indexPath) // in common with cellForItem
var sz = self.modelCell.contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
sz.width = ceil(sz.width); sz.height = ceil(sz.height)
self.sections[indexPath.section].itemData[indexPath.row].size = sz // memoize
return sz
}


Related Topics



Leave a reply



Submit