解決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];//強(qiáng)制繪制
}];
}
- (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];//強(qiáng)制繪制
}];
}