UITableView jumps between positions when activating and deactivating UISearchController
Asked Answered
K

3

8

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.

Kast answered 4/3, 2018 at 17:32 Comment(1)
Is you tableView extending beneath the navigation bar? What values do you have for -edgesForExtendedLayout and -extendedLayoutIncludesOpaqueBars?Scenography
K
4

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.

Kast answered 14/3, 2018 at 23:48 Comment(1)
How can I set 'Extends Edges Under Top Bars' to false?Coif
S
6

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.

Scenography answered 7/3, 2018 at 16:23 Comment(1)
It's default valueVest
J
5

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!

Jasminejason answered 12/3, 2018 at 7:18 Comment(1)
Unfortunately, I did previously give this a try and it just results in the table view not moving with the navigation bar at all.Footless
K
4

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.

Kast answered 14/3, 2018 at 23:48 Comment(1)
How can I set 'Extends Edges Under Top Bars' to false?Coif

© 2022 - 2024 — McMap. All rights reserved.