UICollectionView: Animate cell size change on selection

With the help of Chase Roberts, I now found the solution to my problem. My code basically looks like this: // Prepare for animation [self.collectionView.collectionViewLayout invalidateLayout]; UICollectionViewCell *__weak cell = [self.collectionView cellForItemAtIndexPath:indexPath]; // Avoid retain cycles void (^animateChangeWidth)() = ^() { CGRect frame = cell.frame; frame.size = cell.intrinsicContentSize; cell.frame = frame; }; // Animate [UIView … Read more

UICollectionView flowLayout not wrapping cells correctly

There is a bug in UICollectionViewFlowLayout’s implementation of layoutAttributesForElementsInRect that causes it to return TWO attribute objects for a single cell in certain cases involving section insets. One of the returned attribute objects is invalid (outside the bounds of the collection view) and the other is valid. Below is a subclass of UICollectionViewFlowLayout that fixes … Read more

UICollectionView Layout like SnapChat?

Based on a precedent answer adapted to your issue: -(id)initWithSize:(CGSize)size { self = [super init]; if (self) { _unitSize = CGSizeMake(size.width/2,80); _cellLayouts = [[NSMutableDictionary alloc] init]; } return self; } -(void)prepareLayout { for (NSInteger aSection = 0; aSection < [[self collectionView] numberOfSections]; aSection++) { //Create Cells Frames for (NSInteger aRow = 0; aRow < [[self … Read more

UICollectionView, full width cells, allow autolayout dynamic height?

1. Solution for iOS 13+ With Swift 5.1 and iOS 13, you can use Compositional Layout objects in order to solve your problem. The following complete sample code shows how to display multiline UILabel inside full-width UICollectionViewCell: CollectionViewController.swift import UIKit class CollectionViewController: UICollectionViewController { let items = [ [ “Lorem ipsum dolor sit amet.”, “Lorem … Read more

How to set cell spacing and UICollectionView – UICollectionViewFlowLayout size ratio?

Add these 2 lines layout.minimumInteritemSpacing = 0 layout.minimumLineSpacing = 0 So you have: // Do any additional setup after loading the view, typically from a nib. let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0) layout.itemSize = CGSize(width: screenWidth/3, height: screenWidth/3) layout.minimumInteritemSpacing = 0 layout.minimumLineSpacing = 0 collectionView!.collectionViewLayout = … Read more

targetContentOffsetForProposedContentOffset:withScrollingVelocity without subclassing UICollectionViewFlowLayout

OK, answer is no, there is no way to do this without subclassing UICollectionViewFlowLayout. However, subclassing it is incredibly easy for anyone who is reading this in the future. First I set up the subclass call MyCollectionViewFlowLayout and then in interface builder I changed the collection view layout to Custom and selected my flow layout … Read more

UICollectionView inside a UITableViewCell — dynamic height?

The right answer is YES, you CAN do this. I came across this problem some weeks ago. It is actually easier than you may think. Put your cells into NIBs (or storyboards) and pin them to let auto layout do all the work Given the following structure: TableView TableViewCell CollectionView CollectionViewCell CollectionViewCell CollectionViewCell […variable number … Read more

How to adjust height of UICollectionView to be the height of the content size of the UICollectionView?

I would suggest the following: Add a height constraint to your collection view. Set its priority to 999. Set its constant to any value that makes it reasonably visible on the storyboard. Change the bottom equal constraint of the collection view to greater or equal. Connect the height constraint to an outlet. Every time you … Read more