Paging UICollectionView by cells, not screen
OK, so I found the solution here: targetContentOffsetForProposedContentOffset:withScrollingVelocity without subclassing UICollectionViewFlowLayout
I should have searched for targetContentOffsetForProposedContentOffset
in the begining.
UICollectionView horizontal paging with space between pages
Solution one:
collectionView.isPagingEnabled = false
- add a
minimumLineSpacing
for the distance between pages - implement
targetContentOffsetForProposedContentOffset:withScrollingVelocity:
to move the contentOffset to the closest page. You can calculate the page with simple math based on youritemSize
andminimumLineSpacing
, but it can take a little work to get it right.
Solution Two:
collectionView.isPagingEnabled = true
- add a
minimumLineSpacing
for the distance between pages - the paging size is based on the bounds of the collectionView. So make the collectionView larger then then screenSize. For example, if you have a
minimumLineSpacing
of 10 then set the frame of the collectionView to be {0,-5, width+10, height} - set a contentInset equal to the
minimumLineSpacing
to make the first and last item appear correctly.
Swift - Paging UICollectionView by cell while keeping the cell horizontally centered
You can use insetForSectionAtIndex
to add spacing at first and last cell
UPDATE
You can use scroll view.
First: add leading and trailing to scroll view:
Second: scrollView.clipsToBounds = false
Third: add view to scroll view
func setupScrollView() {
DispatchQueue.main.async {
self.scrollView.layoutIfNeeded() // in order to get correct frame
let numberItem = 6
let spaceBetweenView: CGFloat = 20
let firstAndLastSpace: CGFloat = spaceBetweenView / 2
let width = self.scrollView.frame.size.width
let height = self.scrollView.frame.size.height
let numberOfView: CGFloat = CGFloat(numberItem)
let scrollViewContentSizeWidth = numberOfView * width
self.scrollView.contentSize = CGSize(width: scrollViewContentSizeWidth, height: height)
for index in 0..<numberItem {
let xCoordinate = (CGFloat(index) * (width)) + firstAndLastSpace
let viewFrame = CGRect(x: xCoordinate, y: 0, width: width - spaceBetweenView, height: height)
let view = UIView()
view.backgroundColor = .red
view.frame = viewFrame
self.scrollView.addSubview(view)
}
}
}
UICollectionView Horizontal Paging not centered
Remove spaces between items. For horizontal scrolling collection view set minimum line spacing to 0. You can do this with interface builder or with method of UICollectionViewDelegateFlowLayout
protocol:
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
Another way is making your cell's width less than collectionView's width for a value of horizontal space between items. Then add section insets with left and right insets that equal a half of horizontal space between items. For example, your minimum line spacing is 10:
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 10;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(collectionView.frame.size.width - 10, collectionView.frame.size.height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 5, 0, 5);
}
And third way: manipulate collectionView scroll in scrollViewDidEndDecelerating:
method:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView == self.collectionView) {
CGPoint currentCellOffset = self.collectionView.contentOffset;
currentCellOffset.x += self.collectionView.frame.size.width / 2;
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:currentCellOffset];
[self.collectionView scrollToItemAtIndexPath:indexPath
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:YES];
}
}
Related Topics
Swift 3: Transfer Utility Enumeratetoassignblocks Method Signature
How to Write a Better Data Access Layer with Realm
Whats the Correct Way, Using "Init" or "Didmove"
Sync Video in Avplayerlayer and Avplayerviewcontroller
Cancel All Operations + Afnetworking 3.0
iOS Uitableview Accessibility Order Is Wrong After Setting Accessibilityelements on Uitableviewcell
Swift Package Manager Issues with Unversioned Packages (Example: Firebase-Ios-Sdk)
Can Someone Explain This @Synthesize Syntax
Swift + Firebase Security Not Working
Invalid Transaction Receipt Returned by Appstorereceipturl (Nsdata), in iOS 7
How to Chain Filters in Metal for iOS
Issue Comparing Uicolors in Swift
Xcode Reference a Framework Instead of Link Binary with Libraries
How to Create a Popup Menu in iOS
iOS Today Extension with Core Data
"Googleplus/Googleplus.H File Not Found" When Trying to Build My Project