I have an application that shows a list of items retrieved from the API in a UITableView (let's call this as the MasterList). Upon tapping on an item in the MasterList:
- I push a ViewController (call this as
DetailList) onto the Navigation Controller - Make a network call to get items as per the selection in
MasterList - Display the retrieved items in a UITableView in DetailList
- The network API tells the app whether to show a search-bar or not via a property called
showSearchBar. - The network API takes about 30ms to come back with the response. But at this point when I try to show the UISearchController in the NavigationItem, it fails to show up.
NOTE: This is a simplified code for easy understanding:
override func viewDidLoad() {
super.viewDidLoad()
//Make a network call to get data
viewModel.getData()
}
//Callback from viewModel when data is received
func didGetNetworkResponse() {
configureSearchController()
//other routines to populate UITableView, etc.
}
func configureSearchController() {
//viewModel decides to show search-bar based on API response
//safe to assume the API returns `true`
guard viewModel.shouldShowSearchBar else { return }
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.searchBar.autocapitalizationType = .none
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
searchController.searchBar.placeholder = "Search for items"
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
navigationController?.view.setNeedsLayout()
navigationController?.view.layoutIfNeeded()
}
Whereas the UISearchController shows up correctly if I try to show it in viewDidLoad() without having to wait for the API response. Also all of the above works perfectly well in iOS 13.x
Questions:
- Am I missing something in the code above
- Is it a known problem with UISearchController not showing up in iOS 11 & 12 when the call to set it is delayed (in this case because of network response)
- Why is the difference of behaviour between iOS 13 and iOS 12
Appreciate your help.