Locking a Uisearchbar to the Top of a Uitableview Like Game Center

Locking a UISearchBar to the top of a UITableView like Game Center

Bob's answer is reversed: it ought to be MIN(0, scrollView.contentOffset.y).

Also, in order to properly support resizing (which would occur when rotated), the other frame values should be reused.

-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
{
UISearchBar *searchBar = searchDisplayController.searchBar;
CGRect rect = searchBar.frame;
rect.origin.y = MIN(0, scrollView.contentOffset.y);
searchBar.frame = rect;
}

Locking a UISearchBar to the top of a UITableView

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

UISearchBar on the top of UITableView which can hide but stay close to UINavigationBar

You will need to hide the search bar on your own when you scroll the tableview. So, don't put it as a UITableView header. You could hide it by setting its height to zero. That way if your tableview is set to autoresize it will expand.

I would experiment with having the UITableView and the UISearchBar as peers within another view. The GameCenter image does not have the search bar as the table view header, rather it has them as separate subviews.

You could also look at UISearchDisplayController but I think it doesn't quite have the behaviour that you want.

Edit: This question is basically your question and has some code in the answers.

Can't scroll a UISearchBar into view

Solved by unchecking "hide bars when vertically compact" on the navigation controller.

How to implement a search bar that stays always on top using UISearchDisplayController in iPhone SDK 3.0

I don't have a solution off the top of my head - but I will say the concern you have seems to minimized by the ability for the user to simply tap the top of the screen and have the table zoom to the top (to change search terms if it's wrong).

Search Bar does not receive touches

If you think about it, the table header frame will be outside the table bounds when you scroll.
Try to add it as a subview to the tableView directly and set its frame relative to tableView bounds to keep it sticky.



Related Topics



Leave a reply



Submit