如何更改"取消"按钮,"无结果"标签的字符串在UISearchBar的UISearchDisplayController?
我自己解决了。
取消按钮>
(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];
}
}
}
使用定时器和bool值。如果没有定时器,当"no Results"先显示时,不能更改文本。
- (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];
}
}
}
}
}
要更改"no result"文本,您可以使用:
[self.searchDisplayController setValue:@"my no result text" forKey: @"noResultsMessage"];
我刚刚在iOS8上测试过
感谢ChangUZ找到了解决办法。现在,为了改进,不需要计时器来更改"No Results"标签。
- (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;
}
更改取消按钮文本的简单解决方案:
[self.searchDisplayController.searchBar setValue:@"custom text" forKey:@"cancelButtonText"];
在iOS 10中测试