7

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.

Aloha Silver
  • 1,394
  • 17
  • 35

2 Answers2

10

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.

TomSwift
  • 39,369
  • 12
  • 121
  • 149
  • You'd think one of the UISearchDisplayDelegate methods would allow you a place to override the hiding of the scope bar -- but nothing I tried there would work. Subclassing UISearchBar does the job. – dodgio Nov 22 '11 at 19:50
  • 6
    Whilst this works initially, after clicking the searchBar then cancelling the search in iOS7 the scope bar is then hidden. I cannot find a way to code it in iOS7 to redisplay the scope bar after the UISearchDisplayDelegate hides it. Do you know of a way? – msec Jan 11 '14 at 00:35
0

I know this is old, but if anyone is still searching for an answer here, this method is working for me with iOS 16. The trick seems to be to set it to false first (?).

extension MyViewController: UISearchControllerDelegate {
    func didPresentSearchController(_ searchController: UISearchController) {
        searchController.searchBar.showsScopeBar = false
        searchController.searchBar.setShowsScope(true, animated: false)
    }

    func didDismissSearchController(_ searchController: UISearchController) {
        searchController.searchBar.showsScopeBar = false
        searchController.searchBar.setShowsScope(true, animated: false)
    }
}
mikejonesguy
  • 9,779
  • 2
  • 35
  • 49