UICollectionView - go to the next page

Is it possible to scroll in the UICollectionView using the desired item. scrollToItemAtIndexPath and bind not to the element itself, but to the page of which the element is a part? (I have paging enabled.)

Greetings

+4
source share
2 answers

You need to create NSIndexPath, than to scroll through this index.

 //Mark: - Move to cell when view did appear

overload viewDidAppear() {


    let scrollIndex: NSIndexPath = NSIndexPath(forItem: dayIndex, inSection: monthSection)
    if (// Check you are in range of UIcollectionView count) {
        //Scroll collectionview according to your requirement i.e UICollectionViewScrollPositionCenteredHorizontally or UICollectionViewScrollPosition.Left 
        self.YourCollectionView.scrollToItemAtIndexPath(scrollIndex, atScrollPosition: UICollectionViewScrollPositionCenteredHorizontally, animated: true)
    }
}
+4
source

I ended up using the offsetContent property;

I knew the width of each so-called page.

  • I wrote code to get indexPath for the current day
  • I got the indexPath section
  • I multiplied the section by the width of the view and called it "offset"
  • UICollectionView.contentOffset.x "offset", .

.scrollRectToVisible , , -, contentOffset UICollectionView, .scrollRectToVisible, , - , .

!

+2

Source: https://habr.com/ru/post/1624740/


All Articles