7
votes

I've recently added a UISearchController to my table view and I'm experiencing an animation issue. When the search bar is tapped and becomes active, the table view jumps up to meet the search controller's new (active) position. The problem with this is that the search controller animates to this new position but the table view doesn't so it's quite jarring. Here is a video of the issue.

The top constraint on the table view is set to the view controller's safe area. Below is the code I have written for configuring the search controller:

- (void)configureSearchController {
    UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    searchController.searchResultsUpdater = self;
    searchController.obscuresBackgroundDuringPresentation = NO;
    searchController.searchBar.placeholder = @"Search for any cryptocurrency";
    self.searchController = searchController;
    if (@available(iOS 11.0, *)) {
        self.navigationItem.searchController = searchController;
    } else {
        self.navigationItem.titleView = searchController.searchBar;
    }
    self.definesPresentationContext = YES;
}

Does anyone have any suggestions as to how I can make the transition smooth? Ideally I would like the table view to move up at the same rate as the search controller as this is the default behaviour throughout iOS.

3
Is you tableView extending beneath the navigation bar? What values do you have for -edgesForExtendedLayout and -extendedLayoutIncludesOpaqueBars?pfandrade

3 Answers

6
votes

From the video I believe the table view is not extending beneath the navigation bar. You can probably avoid that jump if you actually allow it to do so.

You should also set

tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic

So that its contentInsets are automatically adjusted.

5
votes

I think, the problem with top constraint to safe area. SearchController couldn't update tableview frame while updating it's navigation bar position. So If you could set the tableview's top constraint to superview, animation could be smooth. Set your tableview constraints as below:

enter image description here

Hopefully, It will work!

4
votes

Found the solution after trying various combinations of UITableView and UIViewController attributes.

I simply had to set Extends Edges Under Top Bars to false while keeping the UITableView constrained to the top of the safe area. The animation is now smooth and follows the UISearchController as you'd expect.