I've got a UITableView in which I set its header to be a search bar.
tableView.tableHeaderView = searchController.searchBar
Everything works according to plan until you click it and it seemingly detaches from the tableView and jumps to the top of the screen. The tableView rows stay in place. Any reason it would do that in iOS 11 and not iOS 10?
I had the exact same issue - weird jumping behaviour of the UISearchBar on iOS11, where on iOS10 everything is fine.
Regarding the advice by Simon Wang above - the problem I had there is that I am not defining my
UISearchBar
inside aUIViewController
, I am instead inside anotherUITableViewCell
- so therefore I don't have access to thenavigationItem
and can't present my search bar that way.Anyway, after much trial and error, the only way I could get things working was to scrape the
UISearchController
and related delegates altogether.Instead I define a
UISearchBar
inside Interface Builder, and define its layout constraints as appropriate. I then make its parent class conform toUISearchBarDelegate
and dosearchBar.delegate = self
I then add a bunch of delegate methods to catch content changes to the search bar, and update my table results accordingly:
And life is good again. Hope this helps!
My first post on SO, i hope i am doing this right.
I had the exact same problem and really struggled with finding a solution. But now i have managed to fix it by subclassing
UISearchController
.This subclass has two properties:
So instead of using the searchBar of UISearchController i can now use the new property searchBarWithCustomSize from my custom searchController, like this:
This makes the searchbar behave correctly when active. You also have more freedom with this searchBarWithCustomSize, for example you can change its frame if needed and so on.
In my implementation i need the delegate to know when the searchbar textfield has been changed but i guess the usage of a delegate is dependent on your situation.
Try this:
It is encouraged to apply the new way to show search bar/search controller on iOS 11. Here is what I have done:
Try this.