iosswiftuisearchcontrolleruisearchdisplaycontroller

How to use Search bar with Navigaitonbar


I have successfully get UISearchController to work in a new project, but when I am trying to do the same thing in my own project, it behaves differently.

This is what I have done to get it to work.

Storyboard:

This is my storyboard

This is my code for my view controller:

class ViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {

    var searchController : UISearchController!

    override func viewDidLoad() {
        super.viewDidLoad()

        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let modalVC =  mainStoryboard.instantiateViewController(withIdentifier: "SearchTableViewController_SBID")
        self.searchController = UISearchController(searchResultsController:  modalVC)

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

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

The result I am getting is, when I click on the search bar, the background will be dimmed, and when I started typing, the tableview controller will be visible, will the search bar is still visible in the navigation bar in the tableview controller.

However, when I try to implement the same thing in my project, when I started typing on the search bar, the table view controller will fill the whole screen, and the navigation bar is not visible.

This is the storyboard for my project:

Storyboard for my project

Notice both projects are similar, except for one of them is Navigation Controller -> Tab Bar View Controller, which doesn't show the navigation bar when I type on the searchbar.

How do I get the navigation bar to show in a project Navigation Controller -> Tab Bar View Controller?

This is what it looks like for 2 projects:

No navigation bar

Working project, visible bar


Solution

  • Storyboard Setup

    enter image description here Coding of both view controller

    class ViewController1: UIViewController,UISearchBarDelegate {
        @IBOutlet weak var lbl1: UILabel!
        var searchBar:UISearchBar? = nil
        override func viewDidLoad() {
            super.viewDidLoad()
    
            //Code to identify the searchbar
            for view in (self.tabBarController?.navigationItem.titleView?.subviews)!
            {
                if view.isKind(of: UISearchBar.self)
                {
                    searchBar = (view as! UISearchBar);
                }
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
    
            //Assign Delegate and blank the search bar text 
            searchBar?.text = ""
            searchBar?.delegate = self
    
        }
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            lbl1.text = "Tab 1 : \(searchText)"
    
        }
    
    }
    class ViewController2: UIViewController,UISearchBarDelegate {
        @IBOutlet weak var lbl1: UILabel!
        var searchBar:UISearchBar? = nil
        override func viewDidLoad() {
            super.viewDidLoad()
    
            //Code to identify the searchbar
            for view in (self.tabBarController?.navigationItem.titleView?.subviews)!
            {
                if view.isKind(of: UISearchBar.self)
                {
                    searchBar = (view as! UISearchBar);
                }
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            searchBar?.text = ""
            searchBar?.delegate = self
        }
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            lbl1.text = "Tab 2 : \(searchText)"
    
        }
    
    }
    

    Result

    enter image description here