Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView jumps between positions when activating and deactivating UISearchController

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.

like image 421
A. Walker Avatar asked Mar 04 '18 17:03

A. Walker


3 Answers

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.

like image 174
pfandrade Avatar answered Nov 18 '22 17:11

pfandrade


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!

like image 45
Shamim Hossain Avatar answered Nov 18 '22 17:11

Shamim Hossain


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.

like image 37
A. Walker Avatar answered Nov 18 '22 16:11

A. Walker