0 Replies
      Latest reply on Jul 9, 2016 5:24 PM by macuser1984
      macuser1984 Level 1 Level 1 (0 points)

        I have a UICollectionViewController set up similar to the code below, and it’s selecting the correct cell, but there’s one problem happening. The problem is that the header is covering the top part of the selected cell when selectItemAtIndexPath is called (meaning that the top edge of the cell is right next to the bottom of the navigation bar, not the bottom of the header). Is there any way to change the .Top scroll position so the selected cell is not covered at all when it's programmatically scrolled to but still have sectionHeadersPinToVisibleBounds set to true? (and my setup is simple enough that I don’t think I need a custom layout)

         

        import UIKit
        private let reuseIdentifier = "CollectionCell"
        private let headerReuseIdentifier = "CollectionHeader"
        class CollectionViewController: UICollectionViewController {
            override func viewWillAppear(animated: Bool) {
                if let layout = collectionViewLayout as? UICollectionViewFlowLayout {
                    layout.sectionHeadersPinToVisibleBounds = true
                }
              
                collectionView?.selectItemAtIndexPath(NSIndexPath(forItem: 20, inSection: 1), animated: true, scrollPosition: .Top)
            }
        
            override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
                return 3
            }
        
            override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                return 100
            }
        
            override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
                let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
                return cell
            }
          
            override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
                let view = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: headerReuseIdentifier, forIndexPath: indexPath)
              
                return view
            }
        
            override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
                return true
            }
        }
        
        

         

        If this helps, the behavior I want is like in the Settings app -> General -> Language & Region -> Region except with a collection view instead of a table view (and UITableView seems to have the behavior I want by default, but a table view isn’t appropriate for the type of content in my view controller).

         

        Thanks!