UISearchControllersearch栏在活动时与第一个单元格重叠

我正在使用UISearchControllersearch我的tableview中的数据。 我不使用表格视图控制器。 我想隐藏导航栏,当我的search栏是活动的,因此我设置self.searchController.hidesNavigationBarDuringPresentation = YES; 是的。

但是,当我这样做,想要search,我的积极search栏覆盖第一个单元格的一部分。 被覆盖部分与状态栏高度相同。

状态栏激活

我尝试了其他的文章和类似的问题,但没有任何帮助我解决这个问题。 然后,我开始玩桌面视图标题大小。 我做到了

  -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 20.0f; } 

结果是,当我点击search栏开始search问题已经不存在了。

搜索控制器头大小为20

但是,当search栏不活动时,search栏和第一个单元格之间会出现一个空白

在这里输入图像说明

任何想法如何解决这个问题?

编辑:添加self.automaticallyAdjustsScrollViewInsets = false;

在这里输入图像说明

这是我设置search栏和viewDidLoad(从苹果的一些例子复制)的东西。

它显示了与未经过滤的数据显示在同一个视图控制器中find的结果。 它还在表头中有它的search栏,直到需要时才隐藏。

 self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; self.searchController.searchResultsUpdater = self; self.tableView.tableHeaderView = self.searchController.searchBar; [self.searchController.searchBar sizeToFit]; // we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables self.searchController.delegate = self; self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES self.searchController.searchBar.delegate = self; // so we can monitor text changes + others // Search is now just presenting a view controller. As such, normal view controller // presentation semantics apply. Namely that presentation will walk up the view controller // hierarchy until it finds the root view controller or one that defines a presentation context. // self.definesPresentationContext = YES; // know where you want UISearchController to be displayed // Hides search bar initially. When the user pulls down on the list, the search bar is revealed. [self.tableView setContentOffset:CGPointMake(0, self.searchController.searchBar.frame.size.height)]; 

我设法通过结合RJiryes答案和滚动到顶部来解决这个问题。

 -(void)willPresentSearchController:(UISearchController *)searchController{ [self.contactsTableView setContentInset:UIEdgeInsetsMake(20, 0, 0, 0)]; [self.contactsTableView setContentOffset:CGPointMake(0.0f, -self.contactsTableView.contentInset.top) animated:YES]; } -(void)willDismissSearchController:(UISearchController *)searchController{ [self.contactsTableView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)]; } 

您可以从顶部添加内容插入,而不是添加标题。

  self.tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0) 

当willDismissSearchController(UISearchController的委托方法)被调用时,将insets返回为0

  self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 

这样,当它不活跃时,你将避免空白。

不是最好的解决scheme,而是解决方法。

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return searchController.isActive ? 20.0f : 0.0f; }

我见过类似的行为。 最有可能的是,你的表视图控制器没有实现heightForHeaderInSection。 细胞的精确高度是未知的,这导致像你这样的问题的数量。

我有同样的麻烦。 而研究没有回答。 我的决定:

1)我用SearchController和UITableviewController,当我在字段上录音时,我有这个用户界面的麻烦:search栏和tableview之间的额外的空白这些家伙指出,你需要分别与UIViewcontroller和tableView使用searchController。 所以我做了

2)有这个问题。 这是解决如下:

  let searchController = UISearchController(searchResultsController: nil) var tableView:UITableView! override func viewDidLoad() { super.viewDidLoad() loadData() setupTableView() setupSearchController() } private func setupTableView(){ tableView = UITableView(frame: CGRect.zero, style: .plain) **//next 2 very important line of code** tableView.autoresizingMask = UIViewAutoresizing() tableView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(tableView) tableView.dataSource = self tableView.delegate = self tableView.register(LetterBrandCell.self, forCellReuseIdentifier: "brandCell") tableView.tableFooterView = UIView() addConstraints() } 

在添加约束的方法中,附加到top&bottomLayoutGuide非常重要,而不仅仅是查看:

 private func addConstraints(){ NSLayoutConstraint(item: tableView, attribute: .top, relatedBy: .equal, toItem: topLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0).isActive = true NSLayoutConstraint(item: tableView, attribute: .bottom, relatedBy: .equal, toItem: bottomLayoutGuide, attribute: .top, multiplier: 1, constant: 0).isActive = true NSLayoutConstraint(item: view, attribute: .leading, relatedBy: .equal, toItem: tableView, attribute: .leading, multiplier: 1, constant: 0).isActive = true NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: tableView, attribute: .trailing, multiplier: 1, constant: 0).isActive = true }