ios – UICollectionView + UISearchController – How you can obtain clean scrolling animation when hiding search bar?

0
62


I discover that, regular implementation for UICollectionView + UISearchController, won’t in a position to obtain clean scrolling animation when hiding search bar.

As you may see within the beneath video, after we scroll upward the web page, it looks as if “the web page has slipped up out of the blue”.

enter image description here

If we evaluate the animation with search bar in iOS Settings web page, the issue is extra apparent. iOS Settings web page in a position to have a clean scrolling expertise.

enter image description here


Implementation

The next is our implementation. The entire workable mission is discovered at : https://github.com/yccheok/demo-uicollectionview-uisearchcontroller

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    let reuseIdentifier = "cell" // additionally enter this string because the cell identifier within the storyboard
    var objects = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "bye"]
    
    non-public lazy var searchController: UISearchController = {
        let searchController = UISearchController(searchResultsController: UIViewController())
        
        searchController.searchResultsUpdater = self
        searchController.obscuresBackgroundDuringPresentation = true
        searchController.searchBar.placeholder = "search_todos"
        searchController.searchBar.delegate = self

        return searchController
    }()
   
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        collectionView.dataSource = self
        collectionView.delegate = self
        
        navigationItem.searchController = searchController
    }
    
    // MARK: - UICollectionViewDataSource protocol
    
    // inform the gathering view what number of cells to make
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
        return self.objects.depend
    }
    
    // make a cell for every cell index path
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        // get a reference to our storyboard cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MyCollectionViewCell
        
        // Use the outlet in our customized class to get a reference to the UILabel within the cell
        cell.label.textual content = self.objects[indexPath.item]
        cell.backgroundColor = .yellow
        
        return cell
    }
    
    // MARK: - UICollectionViewDelegate protocol
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // deal with faucet occasions
        print("You chose cell #(indexPath.merchandise)!")
    }
}

class MyCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var label: UILabel!
}

extension ViewController: UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {
    }
}

extension ViewController: UISearchBarDelegate {
}

Do you will have any thought, how we are able to obtain such a clean scrolling animation? Thanks.