UISearchBar disconnects when using UINavigationController inside UITabBarController

I am trying to implement a search bar on one of the tabs in my UITabBarController, the tab is the UITableViewController in the UINavigationController ... I am following an Apple tutorial. I tried many different options, including the answers here.

The search string is disabled when using the UISearchController with the UITabBarController inside the UINavigationController

I tried to set the following property using

self.definesPresentationContext = true 

or

 self.tabBarController?.definesPresentationContext = true 

Here is my code (from a UITableViewController containing a UISearchBar):

 /// Search controller to help us with filtering. var searchController: UISearchController! /// Secondary search results table view. var resultsTableController: SearchResultsTableController! override func viewDidLoad() { super.viewDidLoad() resultsTableController = SearchResultsTableController() resultsTableController.tableView.delegate = self searchController = UISearchController(searchResultsController: resultsTableController) searchController.searchResultsUpdater = self searchController.searchBar.sizeToFit() self.tableView.tableHeaderView = searchController.searchBar searchController.delegate = self searchController.dimsBackgroundDuringPresentation = true searchController.searchBar.delegate = self // so we can monitor text changes self.definesPresentationContext = true } 

Here's the image of the search bar:

enter image description here

And as soon as I touch it: enter image description here

+5
source share
2 answers

Well, finally, I solved the problem. This line made it work

 self.extendedLayoutIncludesOpaqueBars = true 

My TabBar is not translucent, so I didn’t think it would matter, but I installed it on my UITableviewcontroller (the controller that displays the UISearchController), and now the search displays correctly in the navigation bar. I also had edge extensions in the top and bottom columns set to true (using Interface Builder)

+4
source

Although my search bar worked fine in iOS 9, using sizeToFit in the searchController searchBar and also definesPresentationContext=true on the view controller, which hosts the search results view controller, something changed in iOS 10.

The new fix that works for me is to turn off the “Custom Scroll” in the search results viewer. I just did it in Interface Builder. I had to leave Extend Edges on. Oddly enough, this leads to the Builder interface showing that the table cells are cropped under the navigation bar, but it does not turn off at run time.

0
source

Source: https://habr.com/ru/post/1237715/


All Articles