Rakesha Shastri
Rakesha Shastri

Reputation: 11242

Search Controller in Navigation Item confusing behaviour

So i have a searchcontroller on my navigationItem.

// View controller
var searchController = UISearchController(searchResultsController: nil)

override func viewWillAppear(_ animated: Bool) {
    tableView.reloadData()
    if #available(iOS 11.0, *) {
        navigationController?.navigationBar.prefersLargeTitles = true
    }
    if employeeSearchList.count > 10 {
        if #available(iOS 11.0, *) {
            navigationItem.searchController = searchController
        } else {
            tableView.tableHeaderView = searchController.searchBar
        }
    } else {
        if #available(iOS 11.0, *) {
            let search = UISearchController(searchResultsController: nil)
            navigationItem.searchController = search
            navigationItem.searchController = nil
        } else {
            tableView.tableHeaderView = nil
        }
    }
    UIView.animate(withDuration: 0.50, animations: {
        self.view.layoutIfNeeded()
    })
}

This piece of code runs perfectly. If the count is less than 10, the search controller is set, otherwise it is set to an empty search controller and then set to nil, so that it disappears from the UI.

func viewDidLoad() {
    .
    .
    .
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.tintColor = tintColor

    // Table view editing delegate -> delete operation
    if employeeSearchList.count < 10 {
        if #available(iOS 11.0, *) {
            let search = UISearchController(searchResultsController: nil)
            navigationItem.searchController = search
            navigationItem.searchController = nil
        } else {
            tableView.tableHeaderView = nil
        }
        UIView.animate(withDuration: 0.50, animations: {
            tableView.reloadData()
            self.view.layoutIfNeeded()
        })
    }
    .
    .
    .
}

Now my problem is, when i present a view controller above this one and then dismiss it, the viewWillAppear executes fine but the search controller doesn't show up. But if i push the view controller and come back, it shows up.

What are the main difference between the 2 operations ? (push/pop & present/dismiss)

Upvotes: 2

Views: 3186

Answers (2)

Rakesha Shastri
Rakesha Shastri

Reputation: 11242

It should be:

navigationController.navigationItem.searchController = searchController

instead of:

navigationItem.searchController = searchController

The latter will only take effect the next time navigationController is refreshed/loaded/whatever the appropriate term is?

Upvotes: 1

Alexkater
Alexkater

Reputation: 76

When you present a view controller form other, you are presenting a new viewController hierarchy, I mean, you are outside of the previous navigation controller. If you push a new controller from the navigation controller, you are adding this to the stack, and the navigation bar will be presented.

Upvotes: 0

Related Questions