'UISearchBar not working or disabled after adding Tab Bar or Navigation Controller
I implemented a search bar to filter the result. It was working perfectly but then when I embedded this View Controller with a Navigation Controller, which is also associated with a Tab Bar Controller, the search bar did not respond when I clicked it. It also looks like it was disabled.
My storyboards
My code for the feed view controller:
import UIKit
import AlamofireImage
class RestaurantsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// Outlets
@IBOutlet weak var tableView: UITableView!
// Initiliazers
var restaurantsArray: [Restaurant] = []
// Add Search Bar Outlet + Variable for filtered Results
@IBOutlet weak var searchBar: UISearchBar!
var filteredRestaurants: [Restaurant] = []
override func viewDidLoad() {
super.viewDidLoad()
// Table View
tableView.delegate = self
tableView.dataSource = self
// Search Bar delegate
searchBar.delegate = self
definesPresentationContext = true
// Get Data from API
getAPIData()
}
// Update API results + restaurantsArray Variable + filteredRestaurants
func getAPIData() {
API.getRestaurants() { (restaurants) in
guard let restaurants = restaurants else {
return
}
self.restaurantsArray = restaurants
self.filteredRestaurants = restaurants
self.tableView.reloadData()
self.tableView.rowHeight = 150
}
}
}
// ––––– TableView Functionality –––––
extension RestaurantsViewController {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredRestaurants.count
}
// Configure cell to use [Movie] array instead of [[String:Any]] and Filtered Array
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Create Restaurant Cell
let cell = tableView.dequeueReusableCell(withIdentifier: "RestaurantCell") as! RestaurantCell
// Set cell's restaurant
cell.r = filteredRestaurants[indexPath.row]
return cell
}
// Send restaurant object to DetailViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let cell = sender as! UITableViewCell
if let indexPath = tableView.indexPath(for: cell) {
let r = filteredRestaurants[indexPath.row]
let detailViewController = segue.destination as! RestaurantDetailViewController
detailViewController.r = r
}
}
}
// Add protocol + Functionality for Searching
// UISearchResultsUpdating informs the class of text changes
// happening in the UISearchBar
extension RestaurantsViewController: UISearchBarDelegate {
// Search bar functionality
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText != "" {
filteredRestaurants = restaurantsArray.filter { (r: Restaurant) -> Bool in
return r.name.lowercased().contains(searchText.lowercased())
}
}
else {
filteredRestaurants = restaurantsArray
}
tableView.reloadData()
}
// Show Cancel button when typing
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.searchBar.showsCancelButton = true
}
// Logic for searchBar cancel button
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = false // remove cancel button
searchBar.text = "" // reset search text
searchBar.resignFirstResponder() // remove keyboard
filteredRestaurants = restaurantsArray // reset results to display
tableView.reloadData()
}
}
I did try putting definesPresentationContext in viewDidLoad() but it didn't make any difference for me. I think it's because either the tab bar controller or navigation controller that somehow "disabled" the search bar functionality.
Solution 1:[1]
This may help as you just ignore to input your view to navigationItem
, which
can accept the event.
self.navigationItem.searchController.searchBar = searchBar
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Elevo |