10

How can I change strings of "Cancel" button, "No Results" label in UISearchBar of UISearchDisplayController?

Cœur
  • 37,241
  • 25
  • 195
  • 267
ChangUZ
  • 5,380
  • 10
  • 46
  • 64

4 Answers4

13

I solved it myself.

Cancel Button>

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

No Results Text>

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
    if (!isChangedNoResults) {
        if ([contactManager.filteredPeople count] == 0) {
            [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(changeNoResultsTextToKorean:) userInfo:nil repeats:YES];
        }
    }
}

I use timer and bool value. If no timer, can not change text when "No Results" show first.

- (void)changeNoResultsTextToKorean:(NSTimer *)timer {
    if (isChangedNoResults) {
        [timer invalidate];
    }
    else {
        for (UIView *subview in [self.searchDisplayController.searchResultsTableView subviews]) {
            if ([subview isKindOfClass:[UILabel class]]) {
                UILabel *targetLabel = (UILabel *)subview;
                if ([targetLabel.text isEqualToString:@"No Results"]) {
                    NSLog(@"Changed!");
                    [targetLabel setText:@"_____"];
                    isChangedNoResults = YES;
                    [timer invalidate];
                }
            }
        }
    }
}
ChangUZ
  • 5,380
  • 10
  • 46
  • 64
  • is there anyway to do this without searching the subviews in iOS 5.0 and Arc? – inforeqd Aug 20 '12 at 03:19
  • Maybe this is not needed in iOS5+ – ChangUZ Oct 08 '12 at 04:50
  • I wrote a relevant article on these kinds of issues: http://artsy.github.com/blog/2012/05/11/on-making-it-personal--in-iOS-with-searchbars/ – orta Oct 26 '12 at 11:05
  • Can somebody explain why the timer is needed here? I was curious enough to replace `didLoadSearchResultsTableView:` with `willLoadSearchResultsTableView:` to modify the `No Results` text before it appears, but that doesn't work. – Matthew Quiros Apr 08 '13 at 07:10
  • This code wrote 2 years ago. And now, I develop Android not iOS. So I do not remember exactly... sorry – ChangUZ Oct 28 '13 at 00:58
5

In order to change the "no result" text you can use :

[self.searchDisplayController setValue:@"my no result text"  forKey: @"noResultsMessage"];

I've just tested in iOS8

4

Thank you ChangUZ for finding a way. Now, for improvement, a timer is not needed to alter the "No Results" label.

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        for (UIView *v in controller.searchResultsTableView.subviews) {
            if ([v isKindOfClass:[UILabel self]]) {
                ((UILabel *)v).text = @"_____";
                break;
            }
        }
    });
    return YES;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
0

A simpler solution for changing the Cancel button text:

[self.searchDisplayController.searchBar setValue:@"custom text" forKey:@"cancelButtonText"];

Tested in iOS 10

chengsam
  • 7,315
  • 6
  • 30
  • 38