最近iOS的客户端适配iOS13,需要将原来的UISearchDisplayController替换为UISearchController,加上页面中存在UITableView + MJRefresh的结构,实际调整中出现了各种细节问题,去查资料,现在iOS这生态...确实没查到什么资料...全靠自己瞎几把调,所以为了方便遇到同样问题的同行,避免浪费无谓的时间,就把适配后的代码放出来,且看代码:
@interface YourViewController ()< UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating>
/** 搜索vc */
@property (nonatomic, strong) UISearchController *searchController;
/** 联系人列表 */
@property (nonatomic, strong) UITableView *contactTableView;
/** 搜索内容 */
@property (nonatomic, strong) NSString *searchContent;
/** 正常状态数据源 */
@property (nonatomic, strong) NSMutableArray *contactArr;
/** 搜索状态数据源 */
@property (nonatomic, strong) NSMutableArray *searchedContactArr;
@end
@implementation YourViewController
#pragma mark - 生命周期
- (void)viewDidLoad {
[super viewDidLoad];
//初始化ui
[self initView];
}
#pragma mark - tableview DataSource / delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.searchController.isActive && self.searchContent.length > 0) {
//处于搜索状态且存在搜索内容时
} else {
//正常状态
}
}
//....省略若干其他无关tableview的代理方法,这些代理方法中区分是否搜索状态的处理逻辑都同上numberOfSectionsInTableView方法
- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
//重写这个代理方法,是为了在存在搜索结果时,滑动列表收起键盘
[self.searchController.searchBar resignFirstResponder];
}
#pragma mark - searchBar delegate
/**
* 点击cancle按钮时调用
*/
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
//点击取消按钮后,清空已有搜索结果
self.searchContent = @"";
//清空搜索结果列表
[self.searchedContactArr removeAllObjects];
//刷新tableview
[self.contactTableView reloadData];
}
#pragma mark - UISearchController delegate
- (void)updateSearchResultsForSearchController:(UISearchController *)aSearchController {
//停止刷新状态
[self.contactTableView.mj_header endRefreshing];
if (self.searchController.isActive) {
//搜索状态禁止刷新
self.contactTableView.mj_header.hidden = YES;
} else {
//正常状态允许刷新
self.contactTableView.mj_header.hidden = NO;
}
self.searchContent = aSearchController.searchBar.text;
//调用搜索方法
[self getSearchedContactList];
//得到搜索结果后,刷新UI
[self.contactTableView reloadData];
}
#pragma mark - 私有方法
- (void)initView
{
[self.contactTableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(self.view);
make.top.equalTo(self.mas_topLayoutGuide);
make.bottom.equalTo(self.mas_bottomLayoutGuide);
}];
//关于UISearchController的所有设置
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
//设置代理
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
//进入搜索状态是否隐藏导航栏,yes为隐藏,no为不隐藏;默认是yes
self.searchController.hidesNavigationBarDuringPresentation = YES;
//这个属性忘了啥意思,反正有用
self.searchController.dimsBackgroundDuringPresentation = NO;
//设置下边这个属性是为了返回到前一个页面,使searchBar消失
self.definesPresentationContext = YES;
//设置searchbar相关
self.searchController.searchBar.placeholder = @"搜索联系人";
[self.searchController.searchBar setBackgroundColor:[UIColor whiteColor]];
self.searchController.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchController.searchBar.tintColor = SC_COLOR(kColorStressClickable);
self.searchController.searchBar.delegate = self;
//隐藏searchBar的分割线
self.searchController.searchBar.backgroundImage = [[UIImage alloc] init];
self.searchController.searchBar.frame = CGRectMake(0, 0, SCREEN_WIDTH, 56);
[self.searchController.searchBar sizeToFit];
//设置searchBar中的UItextfield
UITextField *searchBarTextField = [[UITextField alloc] init];
if (@available(iOS 13.0, *)) {
searchBarTextField = self.searchController.searchBar.searchTextField;
searchBarTextField.backgroundColor = [UIColor whiteColor];
} else {
searchBarTextField = [self.searchController.searchBar valueForKey:@"_searchField"];
//设置searchbar的背景色和文本框颜色
for(UIView *view in [[[self.searchController.searchBar subviews] objectAtIndex:0] subviews]){
if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
view.alpha = 0.0f;
} else if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){
view.backgroundColor = [UIColor whiteColor];
}
}
}
[searchBarTextField setFont:[UIFont systemFontOfSize:14]];
searchBarTextField.layer.cornerRadius = 8.0f;//设置圆角具体根据实际情况来设置
searchBarTextField.textColor = SC_COLOR(kColorNormalDividerBlockText); //字体颜色
searchBarTextField.backgroundColor = SC_COLOR(kColorBgLightLightGray); //背景颜色
//下拉刷新
self.contactTableView.mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
self.contactTableView.mj_header.automaticallyChangeAlpha = YES;
self.contactTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//设置列表头部为搜索框
self.contactTableView.tableHeaderView = self.searchController.searchBar;
}
#pragma -mark 懒加载
- (UITableView *)contactTableView
{
if (!_contactTableView) {
_contactTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
[self.view addSubview:_contactTableView];
_contactTableView.dataSource = self;
_contactTableView.delegate = self;
[_contactTableView registerClass:[YourTableViewCell class] forCellReuseIdentifier:cellIndentify];
//这里我也忘了,不懂自己百度吧
if (@available(iOS 11.0, *)) {
_contactTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
}
return _contactTableView;
}
@end
以上关于搜索框的处理的前提是,搜索结果页面和正常展示的页面共用同一套tableview,其他的就不解释了,注释都有...