Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replicate UISearchBar scrolling behavior in Notes.app?

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.

like image 667
Indoor Avatar asked Jul 29 '10 14:07

Indoor


2 Answers

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;
}
like image 171
joerick Avatar answered Nov 15 '22 09:11

joerick


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.

like image 1
Fabiano Francesconi Avatar answered Nov 15 '22 10:11

Fabiano Francesconi