1

I got SearchController to show up with my TableView, this is what is looks like on launch vs when I scroll up:

enter image description hereenter image description here

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/)

enter image description hereenter image description hereenter image description here

Big Chungus
  • 185
  • 1
  • 3
  • 13

1 Answers1

0

You'd probably have to check for the scroll direction of UITableView and disable the UISearchController manually via code. Here's how:

var lastContentOffset: CGFloat = 0 // declare the previous offset for reference

override func viewDidLoad() {
    super.viewDidLoad()
    //...
    searchController = UISearchController(searchResultsController: nil) // remove let
    navigationItem.hidesSearchBarWhenScrolling = true
    //...
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    navigationItem.searchController = lastContentOffset < scrollView.contentOffset.y ? nil : searchController
}
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
  • Inserted this code, but it crashes my project after I try scrolling down again. Is this functionality not included in the default SearchController? – Big Chungus Jul 20 '20 at 14:48
  • You'd have to move the `searchController` declaration outside the function scope to the view controller's scope. – Frankenstein Jul 20 '20 at 14:52
  • Yes, I have `var searchController : UISearchController!` outside of the viewDidLoad function – Big Chungus Jul 20 '20 at 14:54
  • You're declaring a new searchController in viewDidLoad. Initialize the existing one and your issue is fixed. Just remove the `let`. Check my update if it's not clear. – Frankenstein Jul 20 '20 at 15:00
  • Oh, it doesn't crash anymore, but unfortunately still doesn't hide the search after scrolling. It just bounces back as usual. – Big Chungus Jul 20 '20 at 15:06
  • I've updated `scrollViewDidScroll` method. Check it out. It should work. – Frankenstein Jul 20 '20 at 16:00
  • I tried it, didn't seem to work though. I'm confused because everywhere I look people already have this hiding functionality without making additional functions. I made an edit trying some new stuff as well. – Big Chungus Jul 20 '20 at 16:23
  • I tried this code. It worked for me without any issues. – Frankenstein Jul 20 '20 at 16:34
  • As a last resort, I've added 1 more line of code in `viewDidLoad` for you to modify. – Frankenstein Jul 20 '20 at 16:36