Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Scroll at specific item or offset, with UIKit Compositional Layout collectionView?

I have an iOS 14 collectionView, configured with compositional layout.

You can scroll vertically through the collectionView, and you can scroll horizontally within the sections.

I would like to know if there is a way to scroll programmatically, within a section? Something like collectionView.setContent(offset: 100, inSection: 2)

And is there a way to get the contentOffset of a particular section?

I can't find any function to do that.

Sincerely,

Jery

like image 765
darksider Avatar asked Mar 29 '26 01:03

darksider


1 Answers

Its a hack but for such sections the parent is a UIScrollView which you can get a reference to by referencing a cell in the section and then referencing its superview.

Like this:

let cell = collectionView.cellForItem(at: indexPath)!
let scrollView = cell.superview as! UIScrollView
scrollView.setContentOffset(<#CGPoint#>, animated: true)
like image 93
Arbitur Avatar answered Apr 01 '26 15:04

Arbitur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!