Search code examples
iphoneios4

How to have UISearchBar scope bar always visible?


Here's my latest problem with the iPhone SDK.

I've got a UISearchBar and its delegate all set up. Also, when I load my view, I call

self.searchDisplayController.searchBar.showsScopeBar = YES;

That way, when my view is first presented, I see the scope bar, as expected. But if touch inside the search bar and then outside it (or even if a perform a search and then cancel it), the scope bar gets hidden again.

So my question is: is it possible to have the scope bar always visible? Even after performing searches?

Thanks a lot.


Solution

  • The UISearchDisplayController is hiding the scope bar for you.

    The way around this is to subclass UISearchBar and override the implementation of setShowsScopeBar:

    @interface MySearchBar : UISearchBar {
    
    }
    
    @end
    
    @implementation MySearchBar
    
    - (void) setShowsScopeBar:(BOOL) show
    {
        [super setShowsScopeBar: YES]; // always show!
    }
    
    @end
    

    Then, in Interface Builder, change the class of the Search Bar you have in your view (that is associated with the UISearchDisplayController) to the new class type -- MySearchBar in this example.