4

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.

plgrenier
  • 293
  • 3
  • 14

4 Answers4

6

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.

plgrenier
  • 293
  • 3
  • 14
1

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];
 }
0

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.

Josh Gafni
  • 2,831
  • 2
  • 19
  • 32
0

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.

Community
  • 1
  • 1
andrewbuilder
  • 3,629
  • 2
  • 24
  • 46