4

If I embed the ViewController into a Navigation Bar, navigationItem.titleView.resultSearchController?.searchBar will put a search bar into the navigation bar. However, I've created a UISearchController and a UINavigationBar with code. This time, the navBar is showing up, but the searchBar isn't.

resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController?.searchResultsUpdater = locationSearchTable

let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.delegate = self

let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y:0, width: 375, height: 64))
self.view.addSubview(navBar)
//navBar.topItem = resultSearchController?.searchBar
self.navigationItem.titleView = resultSearchController?.searchBar

navBar.topItem = resultSearchController?.searchBar doesn't work because topItem is a String value and resultSearchController?.searchBar is a UIView type. How can I achieve the same effect?

2 Answers 2

7

Create a UINavigationItem instance and add it to the created navigation bar. Add the search controller search bar to the UINavigationItem as titleView.

class SearchViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {

    var searchController: UISearchController!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.addNavigationbar()
    }

    func addNavigationbar() {
        let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60))
        self.view.addSubview(navBar)

        let navigationItem = UINavigationItem(title: "")
        self.searchController = searchControllerWith(searchResultsController: nil)
        navigationItem.titleView = self.searchController.searchBar

        navBar.setItems([navigationItem], animated: false)
        self.definesPresentationContext = true
    }

    func searchControllerWith(searchResultsController: UIViewController?) -> UISearchController {

        let searchController = UISearchController(searchResultsController: searchResultsController)
        searchController.delegate = self
        searchController.searchResultsUpdater = self
        searchController.searchBar.delegate = self
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.dimsBackgroundDuringPresentation = true

        return searchController
    }
Sign up to request clarification or add additional context in comments.

Comments

0
class UIViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {

    var searchController : UISearchController!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.searchController = UISearchController(searchResultsController:  nil)

        self.searchController.searchResultsUpdater = self
        self.searchController.delegate = self
        self.searchController.searchBar.delegate = self

        self.searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.dimsBackgroundDuringPresentation = true

        self.navigationItem.titleView = searchController.searchBar

        self.definesPresentationContext = true
    }

    func updateSearchResults(for searchController: UISearchController) {

    }


}

1 Comment

There is no navigation bar creation in this code. This code works assuming you embed the VC into a nav bar. My question is how do I make the searchBar the titleView of a navBar I created with code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.