When you scroll a tableView down (Notes.app), the search bar stays fixed to top, but when you scroll a tableView up, the search bar is hidden. That's what I want.
Looking at scroll indicator it appears that UISearchBar is a subview of a table's scroll view. But if I add a search bar to a tableView in Interface Builder, then it always scrolls attached to a tableView just like a regular row.
In the table view, put a blank transparent UIView (searchBarPlaceholderView
) into the tableHeaderView slot, that has exactly the dimensions of a UISearchBar. Then add the UISearchBar (searchBar
) to the top-level view.
Then, in your table view delegate, add
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect sbFrame = searchBar.frame;
sbFrame.origin.y = searchBarPlaceholderView.frame.origin.y - scrollView.contentOffset.y;
// it cannot move from the top of the screen
if (sbFrame.origin.y > 0) {
sbFrame.origin.y = 0;
}
searchBar.frame = sbFrame;
}
That's the default behaviour. You just have to drop the bar in the first header of the table (I guess it's the section 0).
However I just created a new application with an UITableViewController and, through the Interface Builder, I dropped the UISearchBar in the upper side of the tableView and it behaves just like the Notes.app application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With