Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the default text of Cancel Button which appears in the UISearchBar +iPhone

I am developing an Application where I wanted to change the text of Search String in the SearchBar. I wanted to change the text of Cancel Button Also which appears next to the SearchBar. Before entering any string in the search bar we wil get the Search String as the default string. I wanted to change the text of that string and when we click on that searchbar we get a cancel button next to searchbar and I wanted to change the text of that cancel button.

like image 899
Pradeep Reddy Kypa Avatar asked Mar 29 '10 06:03

Pradeep Reddy Kypa


4 Answers

Use the appearance proxy:

id barButtonAppearanceInSearchBar = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];

[barButtonAppearanceInSearchBar setBackgroundImage:grayBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[barButtonAppearanceInSearchBar setTitleTextAttributes:@{
                                      NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:20],
                                 NSForegroundColorAttributeName : [UIColor blackColor]
     } forState:UIControlStateNormal];
[barButtonAppearanceInSearchBar setTitle:@"X"];
like image 174
Yariv Nissim Avatar answered Oct 22 '22 16:10

Yariv Nissim


You also need to have the "searchBar setShowsCancelButton" before the procedure.

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    [theSearchBar setShowsCancelButton:YES animated:NO];
    for (UIView *subView in theSearchBar.subviews){
        if([subView isKindOfClass:[UIButton class]]){
            [(UIButton*)subView setTitle:@"Done" forState:UIControlStateNormal];
        }
    }
}

Note also: use UIButton to avoid problems with Apple!

like image 26
David Brownstone Avatar answered Oct 22 '22 15:10

David Brownstone


Solution for iOS 7. All credits for this go to Mr. Jesper Nielsen - he wrote the code.

-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
    UIButton *cancelButton;
    UIView *topView = theSearchBar.subviews[0];
    for (UIView *subView in topView.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            cancelButton = (UIButton*)subView;
        }
    }
    if (cancelButton) {
        [cancelButton setTitle:@"YourTitle" forState:UIControlStateNormal];
    }

}
like image 23
SGI Avatar answered Oct 22 '22 16:10

SGI


If by "Search String", you mean the placeholder, then this should do it:

[searchBar setPlaceholder:@"Whatever you want"];

As for changing the text of the cancel button, that may be a bit more difficult. Apple does not use a standard UIBarButtonItem for this, or even a non-standard UIButton. Instead they use a UINavigationButton for the cancel button in the search bar. Since this is not a documented public class, attempting to modify it could very well get your app rejected from the App Store. If you do want to risk rejection, then you could search through the subviews of searchBar:

for(UIView *view in [searchBar subviews]) {
    if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
        [(UIBarItem *)view setTitle:@"Whatever you want"];
    }
}

Note that the cancel button is loaded lazily, so you will have to do this modification when the search bar is activated by the user.

like image 4
glorifiedHacker Avatar answered Oct 22 '22 15:10

glorifiedHacker