How to assign searchBar storyboards to searchController searchBar property

When I add searchBar as headerView from tableView , all delegate methods start as expected.

  • self.myTableView.tableHeaderView = self.searchController.searchBar;

But if I take the IBOutlet searchBar from the StoryBoard and assign it self.searchController.searchBar , the delegated methods do not start!

  1. _mySearchBar = self.searchController.searchBar;

I also tried mySearchBar in the view and then assigning:

  1. _mySearchBarView = self.searchController.searchBar; But no success!

I'm doing something wrong

+6
source share
2 answers

It seems like the only way to do this is to subclass UISearchController , and then set your own searchBar :

To use a custom subclass of UISearchBar , subclass UISearchController and implement this property to return a custom search bar.

UISearchController searchBar documentation

0
source

Your third method is almost perfect for doing what you want, all you have to do is add searchController.searchBar as the subview view, not the view itself.

 let searchController = UISearchController(searchResultsController: searchResultsController) searchBarContainer.addSubview(searchController.searchBar) 

* searchBarContainer - Your opinion in the storyboard.

This code is kindly provided by mylovemhz, which answered a similar question at this link: Can I use UISearchController with a storyboard?

By sending it here for those who come to this topic when searching.

0
source

All Articles