UICollectionView auto scroll to cell at IndexPath
I've found that scrolling in viewWillAppear
may not work reliably because the collection view hasn't finished it's layout yet; you may scroll to the wrong item.
I've also found that scrolling in viewDidAppear
will cause a momentary flash of the unscrolled view to be visible.
And, if you scroll every time through viewDidLayoutSubviews
, the user won't be able to manually scroll because some collection layouts cause subview layout every time you scroll.
Here's what I found works reliably:
Objective C :
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// If we haven't done the initial scroll, do it once.
if (!self.initialScrollDone) {
self.initialScrollDone = YES;
[self.collectionView scrollToItemAtIndexPath:self.myInitialIndexPath
atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
}
}
Swift :
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if !self.initialScrollDone {
self.initialScrollDone = true
self.testNameCollectionView.scrollToItem(at:selectedIndexPath, at: .centeredHorizontally, animated: true)
}
}
How to automatically scrolling collectionviewcell to nearest cell(previous or next) when selecting a cell
You can just scroll to that indexPath
on didSelectItemAt
Method
var selectedIndex = 0
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "tabCell", for: indexPath) as! tabCollectionViewCell
cell.tabLabel.text = self.tabArray[indexPath.item]
if self.selectedIndex == indexpath.item {
cell.bottomView.isHidden = false
} else {
cell.bottomView.isHidden = true
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.selectedIndex = indexpath.item
self.tabCollectionView.reloadData()
tabCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
Hope this Help!
How to scroll to a particluar index in collection view in swift
In viewDidLoad
of your controller, you can write the code for scrolling to a particular index path of collection view.
self.collectionView.scrollToItem(at:IndexPath(item: indexNumber, section: sectionNumber), at: .right, animated: false)
CollectionView move to next cell automatically swift
Below is code you can try :
/**
Scroll to Next Cell
*/
func scrollToNextCell(){
//get Collection View Instance
let collectionView:UICollectionView;
//get cell size
let cellSize = CGSizeMake(self.view.frame.width, self.view.frame.height);
//get current content Offset of the Collection view
let contentOffset = collectionView.contentOffset;
//scroll to next cell
collectionView.scrollRectToVisible(CGRectMake(contentOffset.x + cellSize.width, contentOffset.y, cellSize.width, cellSize.height), animated: true);
}
/**
Invokes Timer to start Automatic Animation with repeat enabled
*/
func startTimer() {
let timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("scrollToNextCell"), userInfo: nil, repeats: true);
}
UICollectionView scroll to item not working with horizontal direction
For this part:
collectionView.scrollToItem(at: i, at: .top, animated: true)
When the scroll direction is horizontal
you need to use at: left
, at: right
or at: centeredHorizontally
. at: top
is for vertical
direction.
Related Topics
Uiview Vertical Flip Animation
iOS Scrollview Needs Constraint for Y Position or Height
How to Set the Full Width of Separator in Uitableview
Uploading Archive Error: "Missing iOS Distribution Signing Identity for ..."
Take a Picture on iPhone Without Showing Controls
Referring to Weak Self Inside a Nested Block
Uicollectionview Update a Single Cell
How to Call Method from One Class in Another (Ios)
Xcode 5 Crashes -- Xcode Quit Unexpectedly
iOS Localization: Unicode Character Escape Sequences, Which Have the Form '\Uxxxx' Does Not Work
Communicating Between iOS and Android with Bluetooth Le
Atos Cannot Get Symbols from Dsym of Archived Application
What Are the Benefits of Using Storyboards Instead of Xib Files in iOS Programming
Default Tab Bar Item Colors Using Swift Xcode 6
Share Extension to Open Containing App