6

When I add my searchBar as a headerView of tableView, all the delegate methods are fired as expected.

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

But if I take a searchBar IBOutlet from StoryBoard and assign it to self.searchController.searchBar, delegated methods are not fired!

  1. _mySearchBar = self.searchController.searchBar;

I've also tried embedding the mySearchBar into a view and then assign:

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

Am I doing something wrong

Viki
  • 421
  • 1
  • 4
  • 12

2 Answers2

0

It looks like the only way to do this is to subclass UISearchController and then set your custom searchBar:

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

UISearchController searchBar documentation

Jamal
  • 763
  • 7
  • 22
  • 32
JingJingTao
  • 1,760
  • 17
  • 28
  • This is correct. A.) `UISearchController` is not currently available within the storyboard. B.) The only way to implement custom functionality with `UISearchController` or custom appearances is via subclassing. – TheCodingArt Sep 29 '16 at 15:28
0

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

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

*searchBarContainer is your view in Storyboard.

This code is courtesy of mylovemhz who answered a similar question at this link: Can I use a UISearchController with a storyboard?

Reposting it here for those who come to this thread on their search.

hubear
  • 59
  • 2
  • Thank you @peter-duniho for letting me know the best practices for answering a question on stackoverflow. First answer I've ever given! – hubear Jan 21 '18 at 21:23