UISearchDisplayController总结

本文介绍了如何通过重写UISearchDisplayController的方法来取消点击搜索框时导航栏的默认隐藏行为,以及如何自定义无搜索结果时显示的标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 默认情况下,点击搜索框之后会隐藏NavigationBar,如何取消隐藏呢?

最好的处理方式就是重写UISearchDisplayController的
-(void)setActive:(BOOL)visible animated:(BOOL)animated方法:
首先,自定义一个类CustomSearchDisplayController,继承自UISearchDisplayController,然后在.m文件中重写该方法,并在该方法中主动显示navagationBar

#import "CustomSearchDisplayController.h"
@implementation CustomSearchDisplayController 
-(void)setActive:(BOOL)visible animated:(BOOL)animated
{
   [super setActive:visible animated:animated];
   [self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO];
} 
@end

2 当没有匹配的结果时,默认会在tableView上显示一个“No Result”的标签,如果说想自定义这个标签,可以在tableview中循环遍历出该标签,然后按照你的想法去设置:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString];
    if ([filteredListPinYin count] == 0) {
        UITableView *tableView1 = self.searchDisplayController.searchResultsTableView;
        for( UIView *subview in tableView1.subviews ) {
            if( [subview class] == [UILabel class] ) {
                UILabel *lbl = (UILabel*)subview; // sv changed to subview.
                lbl.text = @”没有结果”;
             }
        }
    }
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值