首页 > UISearchDisplayController 去掉背景中的no results文字

UISearchDisplayController 去掉背景中的no results文字

因为要兼容ios7,所以使用UISearchDisplayController,现在问题是,只要在searchBar里输入文字后,背景中会有no results的文字,请问怎么去掉这段文字

如图:


没有数据的时候会出现,设置一个dummy cell即可。


将以下方法放在UISearchBarDelegate代理方法或者UISearchDisplayControllerdelegate代理方法中:

1)如果你想改掉这个字符,换为自己想要的字符串的话可以试试:

  for (UIView *subView in            self.searchDisplayController.searchResultsTableView.subviews) {
    if ([subView isKindOfClass:NSClassFromString(@"UILabel")]) {
        UILabel *label =(UILabel *)subView;
        label.text = @"自定义字符串";
    }
}

2) 如果单纯想覆盖掉这段文字的话,我用过的最好的方法是:

  for (UIView *subView in self.searchDisplayController.searchResultsTableView.subviews) {
    if ([subView isKindOfClass:NSClassFromString(@"UILabel")]) {
        UILabel *label =(UILabel *)subView;
     //自定义一个View
        CGFloat ViewWidth = [UIScreen mainScreen].bounds.size.width*0.64;
        CGFloat ViewHeight = ViewWidth*0.75;
        tempNoResultView = [[UIView alloc]init];
        tempNoResultView.size = CGSizeMake(ViewWidth, ViewHeight);
        tempNoResultView.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, heightToTop);
        tempNoResultView.backgroundColor = [UIColor whiteColor];
        [label addSubview:_tempNoResultView];
    }
}
//相当于遍历出这个label,用一个View把“No Result”遮盖掉。只有这个方法虽然很low但是works well,用其他的方法并不稳定,因为并不建议修改这些东西而且UISearchDisplayController是个坑,这也是为什么后来苹果放弃UISearchDisplayController的原因。

无数据时,设置一个空白 cell。。这样处理。。

【热门文章】
【热门文章】