Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding searchBar to navigationBar in swift

i'm trying to add a searchBar to the navigationBar. i've researched a bit and this is what i've done so far:

The problem is nothing appear to the navigationBar using this code.

    searchBar = UISearchBar(frame: CGRectMake(0, 0, 320, 44))
    searchBar?.delegate = self
    searchBar?.showsCancelButton = true


    searchController = UISearchDisplayController()
    searchController?.delegate = self
    searchController?.searchResultsDelegate = self

    searchController?.displaysSearchBarInNavigationBar = true
like image 905
Peter Pik Avatar asked Oct 15 '14 13:10

Peter Pik


People also ask

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.

How do I add a search bar to UITableView?

Adding Search Bar on TableViewAdd the UISearchBar at the top of your UITableView. … and give the name searchBar. In the ViewController add a Boolean called searching to know when to switch between the full list of items and the list of items from the searching results.

How do I add a navigation bar to a search box in HTML?

To create a search bar in the navigation bar is easy, just like creating another option in the navbar that will search the database. You need to be careful about the timing of placing the search bar. Make sure separately placed in the navbar.


1 Answers

Try this code that worked for me:

 lazy   var searchBars:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 200, 20))

override func viewDidLoad() {
    super.viewDidLoad()
    var leftNavBarButton = UIBarButtonItem(customView: searchBars)
    self.navigationItem.leftBarButtonItem = leftNavBarButton
}
like image 158
Constantin Suiu Avatar answered Oct 26 '22 23:10

Constantin Suiu