解决ios10以下UISearchController偏移的坑
//解决搜索框偏移问题
self.definesPresentationContext = YES;
self.automaticallyAdjustsScrollViewInsets = NO;
//searchBar
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchBar.frame = CGRectMake(0, 0, 0, 44);
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = YES;
self.searchController.searchBar.delegate = self;
[self.searchController.searchBar sizeToFit];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.returnKeyType = UIReturnKeyDone;
self.tableView.tableHeaderView = _searchController.searchBar;
//下面代码解决搜索之后tableview不移动问题
- (void)didPresentSearchController:(UISearchController *)searchController {
[UIView animateWithDuration:0.3 animations:^{
[self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view).offset(20);
}];
[self.tableView layoutIfNeeded];//强制绘制
}];
}
- (void)willDismissSearchController:(UISearchController *)searchController {
[UIView animateWithDuration:0.3 animations:^{
[self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view).offset(64);
}];
[self.tableView layoutIfNeeded];//强制绘制
}];
}