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

UICollectionView horizontal paging with 3 items

Edit: Demo link: https://github.com/raheelsadiq/UICollectionView-horizontal-paging-with-3-items After a lot searching I did it, find the next point to scroll to and disable the paging. In scrollviewWillEndDragging scroll to next cell x. – (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { float pageWidth = 480 + 50; // width + space float currentOffset = scrollView.contentOffset.x; float targetOffset = targetContentOffset->x; …

Read more

How can I enable/disable section headers in UICollectionView programmatically?

You can either use the collectionView:layout:referenceSizeForHeaderInSection: method of the UICollectionViewDelegateFlowLayout and return CGSizeMake(0,0) or set accordingly the headerReferenceSize of UICollectionViewFlowLayout. Edit: headerReferenceSize is actually the property that storyboard uses to show/hide the headers. I’ve added the relevant lines from the Storyboard file With section checkbox on: <collectionViewFlowLayout key=”collectionViewLayout” minimumLineSpacing=”10″ minimumInteritemSpacing=”10″ id=”xAt-Uo-bMl”> <size key=”headerReferenceSize” width=”50″ height=”50″/></collectionViewFlowLayout> …

Read more

UICollection View Flow Layout Vertical Align

Swift 4 with functional oriented approach: class TopAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout { override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { let attributes = super.layoutAttributesForElements(in: rect)? .map { $0.copy() } as? [UICollectionViewLayoutAttributes] attributes? .reduce([CGFloat: (CGFloat, [UICollectionViewLayoutAttributes])]()) { guard $1.representedElementCategory == .cell else { return $0 } return $0.merging([ceil($1.center.y): ($1.frame.origin.y, [$1])]) { ($0.0 < $1.0 ? $0.0 : $1.0, …

Read more