iosiphoneuisearchbaruisearchdisplaycontroller

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


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


Solution

  • 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];
                    }
                }
            }
        }
    }