uicollectionviewlayout
UICollectionViewFlowLayout estimatedItemSize does not work properly with iOS12 though it works fine with iOS 11.*
In my case, I solved this by explicitly adding the following constraints to the cell’s contentView. class Cell: UICollectionViewCell { // … override func awakeFromNib() { super.awakeFromNib() // Addresses a separate issue and prevent auto layout warnings due to the temporary width constraint in the xib. contentView.translatesAutoresizingMaskIntoConstraints = false // Code below is needed to … Read more
Warning: UICollectionViewFlowLayout has cached frame mismatch for index path ‘abc’
This is likely occurring because the flow layout “xyz” is modifying attributes returned by UICollectionViewFlowLayout without copying them And sure enough, that’s just what you are doing: private override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { let attributes = super.layoutAttributesForItemAtIndexPath(indexPath) let distance = CGRectGetMidX(attributes!.frame) – self.midX; var transform = CATransform3DIdentity; transform = CATransform3DTranslate(transform, -distance, 0, -self.width); … Read more
UICollectionView with a sticky header
Simplest solution for iOS 9 + as it doesn’t need of writing subclass of UICollectionViewFlowLayout. In viewDidLoad of viewController with collectionView use following code: let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout // casting is required because UICollectionViewLayout doesn’t offer header pin. Its feature of UICollectionViewFlowLayout layout?.sectionHeadersPinToVisibleBounds = true
UICollectionView Custom Cell to fill width In Swift
You need to do this programatically. Implement UICollectionViewDelegateFlowLayout in your view controller and provide the size in collectionView:layout:sizeForItemAtIndexPath: func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let kWhateverHeightYouWant = 100 return CGSizeMake(collectionView.bounds.size.width, CGFloat(kWhateverHeightYouWant)) } You will also want to call collectionView.collectionViewLayout.invalidateLayout() inside your view controller’s viewWillLayoutSubviews() so that when the … Read more
Custom animation when switching from one UICollectionViewLayout to another?
#define degreesToRadians(x) (M_PI * (x) / 180.0) UICollectionView *collectionView = self.viewController.collectionView; HorizontalCollectionViewLayout *horizontalLayout = [HorizontalCollectionViewLayout new]; NSTimeInterval duration = 2; [collectionView.visibleCells enumerateObjectsUsingBlock:^(UICollectionViewCell *cell, NSUInteger index, BOOL *stop) { CABasicAnimation *rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@”transform.rotation”]; rotationAnimation.toValue = @(degreesToRadians(360)); rotationAnimation.duration = duration; rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; [cell.layer addAnimation:rotationAnimation forKey:@”rotationAnimation”]; }]; [UIView animateWithDuration:duration animations:^ { collectionView.collectionViewLayout = horizontalLayout; … Read more
UICollectionViewFlowLayout Size Warning When Rotating Device to Landscape
I was getting the same warning. Unsatisfied with the “reloadData” approach, I found that calling [self.collectionView.collectionViewFlowLayout invalidateLayout] before setting the frame of the collection view silenced the warning and yielded the expected results.
UICollectionView wrong contentSize on first load, correct after that
So I was seeing a similar issue where contentSize width was always zero. The simple answer is…there is probably no content in the collectionView yet. That was why it’s content size is zero. I was also noticing that sometimes after calling invalidateLayout on my UICollectionView, I was seeing that self.collectionView.collectionViewLayout.collectionViewContentSize was not the same as … Read more
Reorder cells of UICollectionView
I’ve thought a lot about your question and came to following considerations: Subclassing the FlowLayout seems to be the rightest and the most effective way to reorder cells and to make use of flow layout animations. And your approach works, except of two important things: Let’s say you have a collection view with only 2 … Read more