Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I ran into this while tackling a slightly different problem. While using UISearchDisplayController, I want the search bar to be <em>in</em> the navigation bar (not under).</p> <p>It's not hard to put the search bar in the navigation bar (see <a href="https://stackoverflow.com/q/1614687/594211">UISearchBar and UINavigationItem</a>). However, UISearchDisplayController assumes the search bar is always underneath the navigation bar and (as discussed here) insists on hiding the navigation bar when entering search, so things look awful. Additionally, UISearchDisplayController tints the search bar lighter than normal.</p> <p>I found a solution. The trick is to (counter-intuitively) unhook UISearchDisplayController from controlling any UISearchBar at all. If using xibs, this means deleting the search bar instance, or at least unhooking the outlet. Then create your own UISearchBar:</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; UISearchBar *searchBar = [[[UISearchBar alloc] init] autorelease]; [searchBar sizeToFit]; // standard size searchBar.delegate = self; // Add search bar to navigation bar self.navigationItem.titleView = searchBar; } </code></pre> <p>You will need to manually activate the search display controller when the user taps the search bar (in <code>-searchBarShouldBeginEditing:</code>) and manually dismiss the search bar when the user ends searching (in <code>-searchDisplayControllerWillEndSearch:</code>). </p> <pre><code>#pragma mark &lt;UISearchBarDelegate&gt; - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { // Manually activate search mode // Use animated=NO so we'll be able to immediately un-hide it again [self.searchDisplayController setActive:YES animated:NO]; // Hand over control to UISearchDisplayController during the search searchBar.delegate = (id &lt;UISearchBarDelegate&gt;)self.searchDisplayController; return YES; } #pragma mark &lt;UISearchDisplayDelegate&gt; - (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller { // Un-hide the navigation bar that UISearchDisplayController hid [self.navigationController setNavigationBarHidden:NO animated:NO]; } - (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller { UISearchBar *searchBar = (UISearchBar *)self.navigationItem.titleView; // Manually resign search mode [searchBar resignFirstResponder]; // Take back control of the search bar searchBar.delegate = self; } </code></pre>
    singulars
    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.
    3. VO
      singulars
      1. This table or related slice is empty.
    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