skip to Main Content

I currently have a login and signup buttons labeled as 1 and 2. And underneath that, I have a collectionView with two cells (one for the login UI and one for register UI). When I click on the signup button(Label 2), I want to move the collectionView to the next cell.

I tried using the scrollToItem, but it does not do anything. Anyone know why?

 @objc private func didTapRegisterButton() {
        let i = IndexPath(item: 1, section: 0)
        self.onboardingCollectionView.scrollToItem(at: i, at: .right, animated: true)
  }

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Figured it out. My collectionView had isPagingEnabled property as true. The scrollToItem doesn't work if that is set to true.


  2. If you want to scroll between two cells on button click then first you need to uncheck(disable) Scrolling Enabled property and check(enable) Paging Enabled property of collectionView. (With this approach user can’t scroll manually with swipe)

    On button first action,

    if self.currentIndexPath == 0 {
       return
    } else {
       self.currentIndexPath += 1
       self.yourCollectionView.scrollToItem(at: IndexPath(row: self.currentVisibleIndexPath, section: 0), at: .centeredHorizontally, animated: true)
    }
    

    On button second action,

    if self.currentIndexPath == 1 {
       self.currentIndexPath -= 1
       self.yourCollectionView.scrollToItem(at: IndexPath(row: self.currentVisibleIndexPath, section: 0), at: .centeredHorizontally, animated: true)
    } else {
       return
    }
    

    And declare global variable

    var currentIndexPath: Int = 0
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search