plgrenier
plgrenier

Reputation: 293

UIViewController with UITableView and UISearchBar gap issue

I have a UIViewController with a UITableView and a UISearchBar inside. As soon as the searchbar becomes first responder, there's a small gap between the searchbar and the tableview.

Initial stateSearchbar becomes first responder

There's no gap when the UISearchBar is a subview of the UIViewController view instead of being a subview of the UITableView.

Initial stateSearchbar becomes first responder

Obviously, one issue with that approach is that the searchbar stick to the top and doesn't scroll along with other cells, which is not desired.

Any ideas?

Edit:

Two first images correspond to the UISearchBar being a child of the UITableView, where the gap is occurring.

Last two images correspond to the UISearchBar being a child of the UIViewController view, in which case there's no gap.

Upvotes: 4

Views: 1165

Answers (4)

Simon Canil
Simon Canil

Reputation: 41

I had exactly the same issue, however none of the above solutions worked. As per @plgrenier answer, the translucency of the status bar caused the issue.

One way I handled this was to use the search delegates to switch the translucency:

#pragma mark - Table Search Delegates
- (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar{   
      [self.navigationController.navigationBar setTranslucent:YES];
}
-(void) searchBarTextDidEndEditing:(UISearchBar *)searchBar{
  [self.navigationController.navigationBar setTranslucent:NO];
 }

Upvotes: 1

plgrenier
plgrenier

Reputation: 293

The gap is occurring when setting UINavigationBar translucent property to NO. Setting it to YES resolve the issue. Animation is smooth again and there's no gap between UISearchBar and UITableView. I can't explain why, and it appears to be a bug on Apple side as it is easy to reproduce by creating a sample project.

Upvotes: 6

andrewbuilder
andrewbuilder

Reputation: 3799

I suspect it may have something to do with the fact that in a UITableView and using a UITableViewController, you can pull the entire table view and search bar down to reveal a space behind, a background.

When you are inserting a UISearchBar and UITableView into a UIView, the "pull-down" UI is not inherited (as it is unique to UITableViewController) so the "hint" of a "pull-down" space is not there.

To check this and hopefully solve your problem, read my answer to this previous SO question by implementing the UITableView properties setBackgroundView and setBackgroundColor.

Upvotes: 0

Josh Gafni
Josh Gafni

Reputation: 2881

It's a little bit difficult without seeing the code, but perhaps try setting the uisearchbar as the uitableview's header view instead of adding it as a subview.

Upvotes: 0

Related Questions