I got SearchController to show up with my TableView, this is what is looks like on launch vs when I scroll up:
This is perfect since I want it hidden when not in use. However, after showing it, I can't hide it again by scrolling down. How do I get that functionality?
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchControllerDelegate, UISearchResultsUpdating {
@IBOutlet weak var tableView: UITableView!
func updateSearchResults(for searchController: UISearchController) {
let searchBar = searchController.searchBar
filterContentForSearchText(searchBar.text!)
}
var isSearchBarEmpty: Bool {
return searchController.searchBar.text?.isEmpty ?? true
}
func filterContentForSearchText(_ searchText: String) {
filteredStrings = stockArr.filter { (string: String) -> Bool in
return string.lowercased().contains(searchText.lowercased())
}
tableView.reloadData()
}
var filteredStrings: [String] = []
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Candies"
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
}
}
Edit:
I played around with adding
tableView.tableHeaderView = searchController.searchBar
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "hello"
but it doesn't look like the normal SearchControllers out there (I don't get why I can't make it work like everyone else's seems to be working, it should automatically have this functionality based on what I saw on here http://blog.eppz.eu/swiftui-search-bar-in-the-navigation-bar/)