Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locking a UISearchBar to the top of a UITableView

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?

like image 576
KaterinaPetrova Avatar asked Jan 16 '23 03:01

KaterinaPetrova


1 Answers

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;
}
like image 164
KaterinaPetrova Avatar answered Jan 27 '23 15:01

KaterinaPetrova