Skip to content

Instantly share code, notes, and snippets.

@genedelisa
Last active November 22, 2023 22:22
Show Gist options
  • Select an option

  • Save genedelisa/ebcc33c38a521ff8a719 to your computer and use it in GitHub Desktop.

Select an option

Save genedelisa/ebcc33c38a521ff8a719 to your computer and use it in GitHub Desktop.

Revisions

  1. genedelisa revised this gist Jan 25, 2017. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions collectionViewScroll
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,14 @@
    /**
    Scroll to make the the given section header visible.
    The function scrollToItemAtIndexPath will scroll to the item and hide the section header.
    Swift 3.
    */
    func scrollToSection(section:Int) {
    func scrollToSection(_ section:Int) {
    if let cv = self.collectionView {
    let indexPath = NSIndexPath(forItem: 1, inSection: section)
    if let attributes = cv.layoutAttributesForSupplementaryElementOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) {

    let topOfHeader = CGPointMake(0, attributes.frame.origin.y - cv.contentInset.top)
    let indexPath = IndexPath(item: 1, section: section)
    if let attributes = cv.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: indexPath) {
    let topOfHeader = CGPoint(x: 0, y: attributes.frame.origin.y - cv.contentInset.top)
    cv.setContentOffset(topOfHeader, animated:true)
    }
    }
  2. genedelisa created this gist May 27, 2015.
    14 changes: 14 additions & 0 deletions collectionViewScroll
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    /**
    Scroll to make the the given section header visible.
    The function scrollToItemAtIndexPath will scroll to the item and hide the section header.
    */
    func scrollToSection(section:Int) {
    if let cv = self.collectionView {
    let indexPath = NSIndexPath(forItem: 1, inSection: section)
    if let attributes = cv.layoutAttributesForSupplementaryElementOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) {

    let topOfHeader = CGPointMake(0, attributes.frame.origin.y - cv.contentInset.top)
    cv.setContentOffset(topOfHeader, animated:true)
    }
    }
    }