31

I have a UISearchController with a UITableViewController as a searchResultsController, the UISearchBar of this searchController is set to be in the tableHeaderView of my current tableView displayed in my root ViewController. Almost everything is working well, as expected. But in the animation of the UISearchBar (When i click on the searchBar and the UINavigationBar hides and the searchBar goes to the top, as in the UISearchDisplayController) i have a strange behavior. Instead of moving to the position of the UINavigationBar (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 the viewDidLoad instead of init, and the things are just the same. I think that the center of the problem is in the frame of the searchResultsController'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.

lal
  • 7,410
  • 7
  • 34
  • 45
Tiago Maia
  • 616
  • 1
  • 6
  • 20
  • Did you ever figure out this issue? Currently having a similar problem with my search bar. – Lorenzo Jan 03 '15 at 21:18
  • No, 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. – Tiago Maia Jan 04 '15 at 22:08
  • Alright 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 Jan 04 '15 at 22:47
  • 1
    Never mind! I just noticed I missed the line `self.definesPresentationContext = YES;` which ended up being crucial. – Lorenzo Jan 05 '15 at 01:34
  • See if this previous thread is of any help: http://stackoverflow.com/questions/28326269/uisearchbar-presented-by-uisearchcontroller-in-table-header-view-animates-too-fa/28373171#28373171 – Brian Sachetta Mar 10 '15 at 13:44

8 Answers8

23

Add

self.extendedLayoutIncludesOpaqueBars = YES;

on viewDidLoad method

devnomic
  • 697
  • 8
  • 11
  • Working with UITableViewController together with UISearchController, this fixed this issue for me. – Senry May 26 '16 at 15:38
22

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;
Besi
  • 22,579
  • 24
  • 131
  • 223
Piet Grootnoten
  • 982
  • 6
  • 3
  • Amazing this is exactly what I was looking for, thanks a lot. Indeed the search bar should be in the navigation bar. – Vincent Dondain Jul 23 '15 at 22:05
  • This worked for me too, thanks! The obvious question is why? – Dan Sep 29 '15 at 20:49
  • Worked for me too!. Thanks. The apple example does not even talk about this option. – CF_Maintainer Oct 18 '15 at 22:47
  • I had a `UIView` as a container for the search bar, between the nav bar and a table view for results. I had to set `self.definesPresentationContext` to `false` to prevent the container from jumping down by about 44 pixels when the search bar was activated. – Echelon Feb 25 '16 at 18:11
  • self.searchController.hidesNavigationBarDuringPresentation = false; fixed it for me as well! Thank you! – Jonathan Brown Feb 25 '16 at 18:11
  • Perfect..worked for me..! I added searchbar to navigationItem titleview – christijk Nov 16 '16 at 08:56
17

To make this clearer @Lorenzo's answer worked for me.

self.definesPresentationContext = YES;
Cedrick
  • 566
  • 3
  • 13
  • Man you are my hero of the day! – Perjan Duro Jan 29 '16 at 13:45
  • 4
    The 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 set `self.definesPresentationContext` to `false` to prevent the container from jumping down by about 44 pixels when the search bar was activated. – Echelon Feb 25 '16 at 18:13
7

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
}
Inuyasha
  • 2,316
  • 3
  • 13
  • 17
4

In Swift, try:

override func viewDidLoad() {
    edgesForExtendedLayout = []
    searchController.hidesNavigationBarDuringPresentation = false

    // ...
}
xandrefreire
  • 2,276
  • 14
  • 18
1

I noticed that the UISearchController works perfectly in one of the my views but not the other. The problem was with the UITableViewController and not the UIViewController. If you switch to a UIViewController with a UITableView inside it and properly constrained there are no issues. I implemented mine with a XIB and it worked perfectly.

Aaron
  • 252
  • 3
  • 16
  • This comment lead me in the right direction to solve my issue. The constraints described in Option B of this SO answer fixed my issue: https://stackoverflow.com/a/52300159/4704303 – Monica Granbois Jul 23 '19 at 16:26
1

SWIFT 3.01

func willPresentSearchController(searchController: UISearchController){
self.navigationController?.navigationBar.isTranslucent = true
}

func willDismissSearchController(searchController: UISearchController) {
self.navigationController?.navigationBar.isTranslucent = false
}
Yaroslav Dukal
  • 3,894
  • 29
  • 36
0

In my case the searchBar was in the tableHeaderView and there was no NavigationBar on screen. But the SearchBar still animated upwards overlapping the status bar when becoming active. The solution to prevent this was to set:

searchController.hidesNavigationBarDuringPresentation = false

Which is weird because as I said the view controller was not using a navigation bar.

agilityvision
  • 7,901
  • 2
  • 29
  • 28