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 auto scroll to cell at IndexPath

New, Edited Answer: Add this in viewDidLayoutSubviews SWIFT override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() let indexPath = IndexPath(item: 12, section: 0) self.collectionView.scrollToItem(at: indexPath, at: [.centeredVertically, .centeredHorizontally], animated: true) } Normally, .centerVertically is the case ObjC -(void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; NSIndexPath *indexPath = [NSIndexPath indexPathForItem:12 inSection:0]; [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically | UICollectionViewScrollPositionCenteredHorizontally animated:NO]; } Old answer working for … Read more

UICollectionView current visible cell index

indexPathsForVisibleItems might work for most situations, but sometimes it returns an array with more than one index path and it can be tricky figuring out the one you want. In those situations, you can do something like this: CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size}; CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect)); NSIndexPath *visibleIndexPath = … Read more

uicollectionview remove top padding

You can fix top padding issue by considering one of the following method. Method 1: Natural way to fix your problem by setting up your collectionView dimensions properly from StoryBoard. Method 2: **Updated** You can validate collection frame in viewDidLayoutSubviews or viewWillLayoutSubviews override func viewDidLayoutSubviews() { collectionView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: … Read more

Changing cell background color in UICollectionView in Swift

You can use the function collectionView with the parameter didSelectItemAtIndexPath func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { let selectedCell:UICollectionViewCell = myCollectionView.cellForItemAtIndexPath(indexPath)! selectedCell.contentView.backgroundColor = UIColor(red: 102/256, green: 255/256, blue: 255/256, alpha: 0.66) } This creates a constant for the selected UICollectionViewCell, then you just change the background’s color And then for return to the original color … Read more

Make UICollectionView zoomable?

I think what you’re asking for looks like what is done in the WWDC1012 video entitled Advanced Collection Views and Building Custom Layouts (demo starts at 20:20). You basically have to add pinchGesture to you UICollectionView, then pass the pinch properties (scale, center) to the UICollectionViewLayout (which is a subclass of UICollectionViewFlowLayout), your layout will … Read more

UICollectionView with paging – setting page width

Another way is to create a custom UICollectionViewFlowLayout and override the method like so: – (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)offset withScrollingVelocity:(CGPoint)velocity { CGRect cvBounds = self.collectionView.bounds; CGFloat halfWidth = cvBounds.size.width * 0.5f; CGFloat proposedContentOffsetCenterX = offset.x + halfWidth; NSArray* attributesArray = [self layoutAttributesForElementsInRect:cvBounds]; UICollectionViewLayoutAttributes* candidateAttributes; for (UICollectionViewLayoutAttributes* attributes in attributesArray) { // == Skip comparison with non-cell items (headers … Read more

UIRefreshControl with UICollectionView in iOS7

Having the same problem and found a workaround that seems to fix it. This seems to be happening because the UIScrollView is slowing down the tracking of the pan gesture when you pull past the edge of the scrollview. However, UIScrollView is not accounting for changes to contentInset during tracking. UIRefreshControl changes contentInset when it … Read more

UICollectionView and SwiftUI?

iOS 14 and Xcode 12 SwiftUI for iOS 14 brings a new and nativ grid view that is easy to use called LazyVGrid: https://developer.apple.com/documentation/swiftui/lazyvgrid You can start with defining an array of GridItems. GridItems are used to specify layout properties for each column. In this case all GridItems are flexible. LazyVGrid takes an array of … Read more