UISearchBar lock on top of UITableView

I want to get this behavior as follows: I have a UISearchBar, which is a table header view of my table view. When you scroll through a table, the search bar does move, but if you scroll the borders of the table, the search bar never stops touching the navigation bar.

I found a good answer here - UISearchBar lock at the top of a UITableView, for example, Game Center But this does not work on iOS 6 - table header frame manipulations do not work

What is the reason for this?

+4
source share
3 answers

I found a solution that works on iOS 6 and below

Subclass 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; } 
+7
source

If you use the following:

 [_tableView setTableHeaderView:_searchBar]; 

When you view the table view behind the first row, the search bar should also disappear, and not stick to the top of the view.

Without seeing my code, I can only assume that perhaps you added the UISearchBar as the section heading, and not the table heading?

+1
source

What, since iOS6 sdk does not update when scrolling, may have the same optimization. so you need to call tableview layoutSubviews when scrolling.

 -(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; } 
+1
source

Source: https://habr.com/ru/post/1443411/


All Articles