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

醉酒当歌 提交于 2019-12-28 03:39:05

问题


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


回答1:


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



回答2:


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




回答3:


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



回答4:


A simpler solution for changing the Cancel button text:

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

Tested in iOS 10



来源:https://stackoverflow.com/questions/7238878/how-can-i-change-strings-of-cancel-button-no-results-label-in-uisearchbar-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!