Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's one more step if you want to fully emulate the search bars in Game Center.</p> <p>If you start with <strong>friedenberg's</strong> excellent answer, as well as <strong>followben's</strong> modification for iOS 6+ mentioned in the comments, you still need to adjust the functionality when the search bar itself is active.</p> <p>In Game Center, the search bars will scroll with the table as you scroll down, but will remain fixed below the navigation bar when you attempt to scroll up past the boundaries of the table. However, when the search bar is active and search results are being displayed, the search bar no longer scrolls with the table; <em>it remains fixed in place below the navigation bar</em>.</p> <p>Here's the complete code (for iOS 6+) for implementing this. (If you're targeting iOS 5 or below, you don't need to wrap the UISearchBar in a UIView)</p> <p><strong>CustomTableViewController.m</strong></p> <pre><code>- (void)viewDidLoad { UISearchBar *searchBar = [[UISearchBar alloc] init]; ... UIView *tableHeaderView = [[UIView alloc] initWithFrame:searchBar.frame]; [tableHeaderView addSubview:searchBar]; [self.tableView setTableHeaderView:tableHeaderView]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { UISearchBar *searchBar = self.tableView.tableHeaderView.subviews.lastObject; CGRect searchBarFrame = searchBar.frame; /* * In your UISearchBarDelegate implementation, set a boolean flag when * searchBarTextDidBeginEditing (true) and searchBarTextDidEndEditing (false) * are called. */ if (self.inSearchMode) { searchBarFrame.origin.y = scrollView.contentOffset.y; } else { searchBarFrame.origin.y = MIN(0, scrollView.contentOffset.y); } searchBar.frame = searchBarFrame; } - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { self.inSearchMode = YES; } - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { self.inSearchMode = NO; } </code></pre> <p>Voilà! Now, when the search bar is inactive it will move with the table, and remain fixed when attempting to move beyond the table boundaries. When active, it will remain fixed in place, just like in Game Center.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload