UISearchController searchBar in tableHeaderView animating out of the screen
Solution 1
Add
self.extendedLayoutIncludesOpaqueBars = YES;
on viewDidLoad
method
Solution 2
Have you tried to set the hidesNavigationBarDuringPresentation to false? Solved my headache..
self.searchController.hidesNavigationBarDuringPresentation = false;
Putting the searchbar in the navigation bar gives a more solid user experience in my opinion (for iphone)
self.navigationItem.titleView = self.searchController.searchBar;
Solution 3
To make this clearer @Lorenzo's answer worked for me.
self.definesPresentationContext = YES;
Solution 4
Try this out:
First you need to delegate the
UISearchControllerDelegate
For Swift
func willPresentSearchController(searchController: UISearchController) {
self.navigationController?.navigationBar.translucent = true
}
func willDismissSearchController(searchController: UISearchController) {
self.navigationController?.navigationBar.translucent = false
}
Solution 5
In Swift, try:
override func viewDidLoad() {
edgesForExtendedLayout = []
searchController.hidesNavigationBarDuringPresentation = false
// ...
}
Tiago Maia
I'm a back-end developer working with Laravel. I also have experience developing apps for iOS.
Updated on June 14, 2022Comments
-
Tiago Maia almost 2 years
I have a
UISearchController
with aUITableViewController
as asearchResultsController
, theUISearchBar
of thissearchController
is set to be in thetableHeaderView
of my currenttableView
displayed in my root ViewController. Almost everything is working well, as expected. But in the animation of theUISearchBar
(When i click on the searchBar and theUINavigationBar
hides and the searchBar goes to the top, as in theUISearchDisplayController
) i have a strange behavior. Instead of moving to the position of theUINavigationBar
(y: 0), it jumps out of the screen and than starts the animation that shows the cancel button. I tried moving my instantiate code to theviewDidLoad
instead ofinit
, and the things are just the same. I think that the center of the problem is in the frame of thesearchResultsController
's view, but i'm not sure about that(I tried setting the frame, without success). Everything that i am doing is in pure code.Here is the relevant part of the code:
- (void) viewDidLoad { [super viewDidLoad]; // search controller setup self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsController]; self.searchController.delegate = self; self.searchController.searchResultsUpdater = self; self.searchController.searchBar.delegate = self; [self.searchController.searchBar sizeToFit]; self.tableView.tableHeaderView = self.searchController.searchBar; self.searchController.definesPresentationContext = YES; }
And i have a lazy load for the
searchResultsController
:- (UITableViewController *)searchResultsController { if (_searchResultsController == nil) { _searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain]; _searchResultsController.tableView.delegate = self; _searchResultsController.tableView.dataSource = self; } return _searchResultsController; }
I've downloaded the sample code from apple, but they use storyBoards and a xib for UITableViewCell, the SearchController works perfectly in the project. Does anyone had the same problem? How can i fix this? Any solutions or suggestions would be appreciated.
Thanks for the attention.
-
Lorenzo over 9 yearsDid you ever figure out this issue? Currently having a similar problem with my search bar.
-
Tiago Maia over 9 yearsNo, Lorenzo. Unfortunately i used the deprecated api for this issue. With storyboards it seems to work in a good manner(i tried the example app from apple), but in the code i got this weird error.
-
Lorenzo over 9 yearsAlright thanks anyways. Could you elaborate on the method you used that worked? I've been looking at Apple's example as well and they use storyboards for the table and detail view but all of the search bar components are done in code, is this what you did?
-
Lorenzo over 9 yearsNever mind! I just noticed I missed the line
self.definesPresentationContext = YES;
which ended up being crucial. -
Brian Sachetta about 9 yearsSee if this previous thread is of any help: stackoverflow.com/questions/28326269/…
-
-
Vincent Dondain almost 9 yearsAmazing this is exactly what I was looking for, thanks a lot. Indeed the search bar should be in the navigation bar.
-
Dan over 8 yearsThis worked for me too, thanks! The obvious question is why?
-
CF_Maintainer over 8 yearsWorked for me too!. Thanks. The apple example does not even talk about this option.
-
Perjan Duro over 8 yearsMan you are my hero of the day!
-
Echelon about 8 yearsI had a
UIView
as a container for the search bar, between the nav bar and a table view for results. I had to setself.definesPresentationContext
tofalse
to prevent the container from jumping down by about 44 pixels when the search bar was activated. -
Jonathan Brown about 8 yearsself.searchController.hidesNavigationBarDuringPresentation = false; fixed it for me as well! Thank you!
-
Echelon about 8 yearsThe opposite worked for me! I had a
UIView
as a container for the search bar, between the nav bar and a table view for results. I had to setself.definesPresentationContext
tofalse
to prevent the container from jumping down by about 44 pixels when the search bar was activated. -
Senry almost 8 yearsWorking with UITableViewController together with UISearchController, this fixed this issue for me.
-
christijk over 7 yearsPerfect..worked for me..! I added searchbar to navigationItem titleview
-
Monica Granbois almost 5 yearsThis comment lead me in the right direction to solve my issue. The constraints described in Option B of this SO answer fixed my issue: stackoverflow.com/a/52300159/4704303