How to programmatically scroll through a collection view?

uicollectionview get scroll position swift
scroll to index in collection view
uicollectionview scroll one cell at a time swift
collection view cell scroll
collectionview scrolltoitem not working swift
swift collection view scroll to item not working
uicollectionview center cells horizontally swift
auto scroll uicollectionview swift

I have a collection view in my view. In the cells are image views and it has one section.

I now want to scroll programmatically through the images. I want the animation to happen endless so it starts with item one after reaching item 10.

It also would be helpful if you provide some method where to put animations like the cells getting bigger after getting programmatically swiped in from right.

There are two methods that could help you achieve this:

func scrollToItemAtIndexPath(indexPath: NSIndexPath,
        atScrollPosition scrollPosition: UICollectionViewScrollPosition,
                animated animated: Bool)

or

func setContentOffset(contentOffset: CGPoint,
         animated animated: Bool)

Both are on UICollectionView so you can use whatever seems more convenient. To create a custom animation for this however is more difficult. A quick solution depending on what you need could be this answer.

Swift 4, iOS 11:

// UICollectionView method
func scrollToItem(at indexPath: IndexPath, 
                  at scrollPosition: UICollectionViewScrollPosition,  
                  animated: Bool)

// UIScrollView method
func setContentOffset(_ contentOffset: CGPoint, animated: Bool)

How to Scroll CollectionView to bottom Programmatically, 6 Answers. 6. order by. active, oldest, votes. up vote 9  How to: Navigate Through the Objects in a Data CollectionView. Views allow the same data collection to be viewed in different ways, depending on sorting, filtering, or grouping. Views also provide a current record pointer concept and enable moving the pointer.

Swift 5

Based on Mr.Bean's answer, here is an elegant way using UICollectionView extension:

extension UICollectionView {
    func scrollToNextItem() {
        let contentOffset = CGFloat(floor(self.contentOffset.x + self.bounds.size.width))
        self.moveToFrame(contentOffset: contentOffset)
    }

    func scrollToPreviousItem() {
        let contentOffset = CGFloat(floor(self.contentOffset.x - self.bounds.size.width))
        self.moveToFrame(contentOffset: contentOffset)
    }

    func moveToFrame(contentOffset : CGFloat) {
        self.setContentOffset(CGPoint(x: contentOffset, y: self.contentOffset.y), animated: true)
    }
}

Now you can use it wherever you want:

collectionView.scrollToNextItem()
collectionView.scrollToPreviousItem()

scrollToItem(at:at:animated:), Scrolls the collection view contents until the specified item is visible. SDKs. iOS 6.0+; Mac Catalyst 13.0+; tvOS 9.0+. Scrolling. When a user swipes to initiate a scroll, the end position of the scroll can be controlled so that items are fully displayed. In addition, CollectionView defines two ScrollTo methods, that programmatically scroll items into view. One of the overloads scrolls the item at the specified index into view,

By far this is the best approach i have encountered, the trick is to scroll to the frame which is containing next objects. Please refer the code

/* -------------- display previous friends action ----------------*/
@IBAction func actionPreviousFriends(_ sender: Any) {

    let collectionBounds = self.collectionView.bounds
    let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x - collectionBounds.size.width))
    self.moveToFrame(contentOffset: contentOffset)
}

/* -------------- display next friends action ----------------*/
@IBAction func actionNextFriends(_ sender: Any) {

    let collectionBounds = self.collectionView.bounds
    let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x + collectionBounds.size.width))
    self.moveToFrame(contentOffset: contentOffset)
}

func moveToFrame(contentOffset : CGFloat) {

    let frame: CGRect = CGRect(x : contentOffset ,y : self.collectionView.contentOffset.y ,width : self.collectionView.frame.width,height : self.collectionView.frame.height)
    self.collectionView.scrollRectToVisible(frame, animated: true)
}

How to force UICollectionView to always scroll item to center?, I'd like to be able to scroll any cell in my UICollectionView (FlowLayout) to center. allow to scroll if there are few items that fit into visible part of collectionView. To find the last item in the observable collection and then scroll down to it, you can use: lstComputers.ScrollIntoView(lstComputers.Items(MyData.Count - 1)) Where MyData is used in place of the Items.Count of the ListBox.

You can use this UICollectionView extension (Swift 4.2)

        extension UICollectionView {

        func scrollToNextItem() {
            let scrollOffset = CGFloat(floor(self.contentOffset.x + self.bounds.size.width))
            self.scrollToFrame(scrollOffset: scrollOffset)
        }

        func scrollToPreviousItem() {
            let scrollOffset = CGFloat(floor(self.contentOffset.x - self.bounds.size.width))
            self.scrollToFrame(scrollOffset: scrollOffset)
        }

        func scrollToFrame(scrollOffset : CGFloat) {
            guard scrollOffset <= self.contentSize.width - self.bounds.size.width else { return }
            guard scrollOffset >= 0 else { return }
            self.setContentOffset(CGPoint(x: scrollOffset, y: self.contentOffset.y), animated: true)
        }
    }

And the usage will be like

yourCollectionView.scrollToNextItem()
yourCollectionView.scrollToPreviousItem()

How to programmatically scroll through a collection view?, I have a collection view in my view. In the cells are image views and it has one section. I now want to scroll programmatically through the images. I want the  Use this method when initializing a collection view object programmatically. If you specify nil for the layout parameter, you must assign a layout object to the collectionViewLayout property before displaying the collection view onscreen. If you do not, the collection view will be unable to present any items onscreen.

Creating a horizontal UICollectionView with dynamic cell size, It's important to have a fixed height for this collection view as it will scroll in one direction. to your view controller using the story board or programmatically. I would like to scroll the UICollectionView to the bottom so the last item is in the view. I have tried to use scrollToItemAtIndexPath but it does not seem to be working. I want this to happen afte

How do I create a UICollectionView with standard layout , In your initializer you registered a specific cell with the collection view controller. That's basically telling the collectionview to get a few reay for  Tech support scams are an industry-wide issue where scammers trick you into paying for unnecessary technical support services. You can help protect yourself from scammers by verifying that the contact is a Microsoft Agent or Microsoft Employee and that the phone number is an official Microsoft global customer service number.

UICollectionView extension to programmatically scroll to last item in , a collection view in my view. In the cells are image views and it has one section​. I now want to scroll programmatically through the images. Make sure to connect this collection view outlet to the cell’s collection view in the storyboard. This is a standard IBOutlet, except I marked it as private.This is to create a separation of concerns – our view controller shouldn’t be accessing the collection view through the table view cell.

Comments
  • Both methods would work in the way that they are scrolling but have you an idea to let it scroll from item 1 to 2, to 3, to 4 and after 10 to 1 and that as a uniformly accelerated motion that happens endless. I mean that the things happening when you scroll with your finger happen programmatically.
  • Then you could use a NSTimer to perform a repeated task and call setContentOffset with a value incremented after each call.
  • Is it possible that the timer fires after the completion of scrolling to a certain item? Sth. like waiting for completion and then fire?
  • I don't think that is possible, but you do not need that if you scroll without animation and the timer loop is really small and also the value you increment with is small it should look smooth.
  • So I need to measure the time it takes to perform the scrolling.
  • should check guard contentOffset <= self.contentSize.width - self.bounds.size.width else { return } before ScrollToNextItem.