2

I'm following the raywenderlich.com tutorial to show a search bar for my table, but I can't see it anywhere (yes, I scrolled up)

table at launch after I scroll up

Here's my code:

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
  
    }
}

Please help me, I've tried every StackOverflow solution and nothing works, this code didn't make a difference from the normal tableview I had before.

Big Chungus
  • 185
  • 1
  • 3
  • 13

1 Answers1

2

You're probably using the ViewController without embedding it into a UINavigationController. You've to embed it in UINavigationController to make it show up in the UINavigationBar. So while using ViewController use it as follows, if you're using it programmatically.

UINavigationController(rootViewController: ViewController())

If the ViewController is created in storyboard then Use Editor-> Embed In -> Navigation Controller.

Frankenstein
  • 15,732
  • 4
  • 22
  • 47