Blog

UISearchBar Tricks – Showing The Cancel And The Scope Bar Only When Editing

While working on a particular UISearchBar I ran into 2 little problems, both easy to fix but I thought of posting them in case someone is stuck. What I needed was to make the cancel button and the scope bar to be displayed only while editing. I believe that this should be a pretty common thing to do, and the first part is pretty straight forward:

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
	searchBar.showsCancelButton = YES;
	return YES;
 }

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
	searchBar.showsCancelButton = NO;
	return YES:
}

That is pretty simple, specially if you don’t forget to associate the UISearchBar’s delegate!

Next, I tried the same with the scope bar but it didn’t work – the scope bar wouldn’t appear. During my testing, I tried to start with the scope bar displayed and then hiding it after the first edit… it worked but when disappearing the search bar would preserve the height as if it was still showing the scope bar!

The final solution is a simple call to [searchBar sizeToFit] and everything works as smooth as it should. Here is the complete code with the small bonus of animating the cancel button’s (dis)appearance:

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
	searchBar.showsScopeBar = YES;
	[searchBar sizeToFit];

	[searchBar setShowsCancelButton:YES animated:YES];

	return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
	searchBar.showsScopeBar = NO;
	[searchBar sizeToFit];

	[searchBar setShowsCancelButton:NO animated:YES];

	return YES;
}

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: ,

11 Responses to “UISearchBar Tricks – Showing The Cancel And The Scope Bar Only When Editing”

  1. Austin says:

    Wow, this was a lucky find. Thanks!

  2. Thierry says:

    easy… how do you change the appearance of the scopebar ? (background) thanks again!

  3. WireBear says:

    Really helpful, thanks!

  4. Jedrzej says:

    That helped me. Thanks!

  5. Simon says:

    Fantastic! Thanks! I was just trying to hide the searchBar when the user touched the background – had searchBar.hidden = YES for when the cancel button was clicked, and when the user selected a row from the table, but couldn’t find that anywhere. This does all three…

  6. Praveen says:

    But i have one problem whenever i [searchBar resignFirstResponder] inside – (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar delegate method it hides the scopebar even though i am trying to set it back by saying [searchBar setShowsScopeBar:YES];
    – (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    searchBar.showScopeBar = YES
    //[self setShowsScopeBar:YES]; // I tried it explicitly, but did not help
    }
    – (void) setShowsScopeBar:(BOOL) show
    {
    [self._searchBar setShowsScopeBar: YES]; // always show!
    }

    I want my scope bar always visible.

  7. bob says:

    In your example, searchBarShouldBeginEditing: and searchBarShouldEndEditing: are declared to return a BOOL but you do not return anything.

  8. Ben says:

    Thanks! This was helpful!

    Also this will work as well as animating the appearance/disappearance of the cancel button:

    – (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES animated:YES];
    return YES;
    }

    – (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:NO animated:YES];
    return YES;
    }

  9. Ted says:

    Does not seem to work in iOS 7

  10. beerok says:

    Good tutorial in searchbar function for iOS, Thank you!!!

  11. developer says:

    I’m really new to iOS development so forgive my naive question.
    I’m trying to add a search bar to a table view in an app and the example code I’m referring to to help me uses a scope bar but I cannot find anything that tells me what the scope bar actually does. I can see the scope bar titles section in the Xcode Attributes inspector window but nothing to show me what they are for or even any code to use them. Any help would be greatly appreciated as would a good example of creating a view search. Thanks

Leave a Reply

For spam filtering purposes, please copy the number 4162 to the field below: