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
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.
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With