4

I want to get the behavior like this: I have a UISearchBar, which is a table header view of my tableview. When scrolling the table, the search bar does indeed move, but if you scroll above the boundaries of the table, the search bar never stops touching the navigation bar.

I found a good answer here - Locking a UISearchBar to the top of a UITableView like Game Center But it not works on iOS 6 - manipulations with table view header frame don't work

What can be the reason of this?

Community
  • 1
  • 1
KaterinaPetrova
  • 462
  • 4
  • 12

3 Answers3

7

I found a solution, which works on iOS 6 and lower

Make a subclass of UITableView and override layoutSubviews method

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGRect rect = self.tableHeaderView.frame;
    rect.origin.y = MIN(0, self.contentOffset.y);
    self.tableHeaderView.frame = rect;
}
KaterinaPetrova
  • 462
  • 4
  • 12
1

If you use the following:

[_tableView setTableHeaderView:_searchBar];

When you scroll the table view beyond the first row, the search bar should also disappear rather than sticking to the top of the view.

Without seeing your code, I can only assume that perhaps you have added the UISearchBar as your section header rather than the table header?

Zack Brown
  • 5,990
  • 2
  • 41
  • 54
  • Thanks, but that's not exactly i'm looking for. I also use setTableHeaderView method, but i want to change TableHeaderView behavior a little (and i did on iOS 5.x) - it should scroll with all table content (this is the default behavior), as you said, but should stay on top if you scroll above the boundaries. http://i.stack.imgur.com/w2sVr.jpg - this picture explains better (I want the same as in Game Center) – KaterinaPetrova Nov 01 '12 at 11:22
1

That because iOS6 sdk not update when scrolling, maybe same optimize. so need to call tableview layoutSubviews when scroll.

-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
{
    UISearchBar *searchBar = searchDisplayController.searchBar;
    CGRect rect = searchBar.frame;
    rect.origin.y = MIN(0, scrollView.contentOffset.y);
    [scrollView layoutSubviews];
    searchBar.frame = rect;
}
user501836
  • 1,106
  • 1
  • 12
  • 20
  • I had the same problem. I had used similar code (without the layoutSubviews) in iOS5 that worked fine, but not in iOS6. Adding the layoutSubviews code fixed the problem. – Clinemi Nov 30 '12 at 04:28
  • Doing this seems to cause the searchbar to lose its touch event. Instead, the covered up tableview cell gets called. Anyone see this? – dnstevenson May 07 '13 at 21:40