Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //: Playground - noun: a place where people can play
- import UIKit
- var str = "Hello, playground"
- let searchController = UISearchController(searchResultsController: nil)
- //in viewdidload
- searchController.searchBar.tintColor = .white
- searchController.searchBar.barTintColor = .white
- searchController.searchResultsUpdater = self
- searchController.searchBar.keyboardAppearance = .dark
- searchController.definesPresentationContext = false
- searchController.dimsBackgroundDuringPresentation = false
- UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self] ).tintColor = UIColor.white
- if #available(iOS 11.0, *) {
- self.navigationItem.searchController = searchController
- } else {
- // self.navigationItem.titleView = searchController.searchBar
- // Fallback on earlier versions
- }
- extension ViewController: UISearchResultsUpdating {
- func updateSearchResults(for searchController: UISearchController) {
- let oldFilteredNames = self.filteredGuests
- if (searchController.searchBar.text?.isEmpty)! {
- self.filteredGuests = guests
- } else {
- self.filteredGuests = guests.filter({ (user) -> Bool in
- return (user.name ?? "").lowercased().contains(searchController.searchBar.text!.lowercased()) || (user.surname ?? "").lowercased().contains(searchController.searchBar.text!.lowercased())
- })
- }
- self.collectionView.performBatchUpdates({
- for (oldIndex, oldUser) in oldFilteredNames.enumerated() {
- if self.filteredGuests.contains(oldUser) == false {
- let indexPath = IndexPath(item: oldIndex, section: 0)
- self.collectionView.deleteItems(at: [indexPath])
- }
- }
- for (index, name) in self.filteredGuests.enumerated() {
- if oldFilteredNames.contains(name) == false {
- let indexPath = IndexPath(item: index, section: 0)
- self.collectionView.insertItems(at: [indexPath])
- }
- }
- }, completion: nil)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement