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 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];
}
}
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 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.
Paging with left inset on small cells in UICollectionView
i created a custom collection view layout for paging by cell instead of screen
i also outline a few different ways to customize it with examples in the README
check it out here
let me know if you have any questions about it or need more help
Swift: UICollectionView Paging Enabled
What ever, I can get from your question. I think you want to know on which page it is. So, Here is how i did it:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
for cell in colViewSol.visibleCells {
let indexPath = colViewSol.indexPath(for: cell)
// here add the code whatever you want to do
}
}
Related Topics
How to Set an Nscalendarunitminute Repeatinterval on iOS 10 Usernotifications
Uiscrollview Pauses Nstimer While Scrolling
Swift JSON Error:Could Not Cast Value of Type '_Nsdictionarym' to 'Nsarray'
Swift - Tableview Row Height Updates Only After Scrolling or Toggle Expand/Collapse
Xmlparser.Sharedparser.Decode() in Swift3
Make Timer Run on Background iOS for More Than 3 Minutes
JSON Text Did Not Start with Array or Object and Option to Allow Fragments Not Set
Block_Release Deallocating UI Objects on a Background Thread
Customize Apps Gallery in Appstore Page
Ipa Generated by Swift Is So Big, About 5Mb
How Can Split from String to Array by Chunks of Given Size
Reading Text and Images from a PDF Document in iOS
Clang(Llvm) Compile with Frameworks
iOS Multiline Label in Interface Builder