UISearchController搜索栏在活动时重叠第一个单元格 [英] UISearchController search bar overlap first cell when active

查看:110
本文介绍了UISearchController搜索栏在活动时重叠第一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UISearchController 来搜索我的tableview中的数据。我没有使用表视图控制器。我想在搜索栏处于活动状态时隐藏导航栏,因此我将 self.searchController.hidesNavigationBarDuringPresentation = YES; 设置为是。

I am using UISearchController to search for data in my tableview. I am not using table view controller. I would like to hide the navigation bar when my search bar is active therefore I set self.searchController.hidesNavigationBarDuringPresentation = YES; to yes.

但是当我这样做并想要搜索时,我的主动搜索栏会覆盖第一个单元格的一部分。被覆盖的部分与状态栏具有相同的高度。

However when I do this and want to search, my active search bar covers part of the first cell. the covered part has same height as status bar.

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

I tried other articles and questions similar to this one but nothing helped me to solve it. Then I started to play with the table view header size. I did this

 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

  return 20.0f;
}

结果是当我点击搜索栏时开始搜索问题不是那里了。

The result was that when I tapped search bar an started to search problem was not there anymore.

但是当搜索栏未激活搜索栏和第一个单元格之间存在误差

However when the search bar is not active there was a gab between searchbar and first cell

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

Any ideas how to fix this issue?

编辑:添加 self.automaticallyAdjustsScrollViewInsets = false;

推荐答案

这就是我设置搜索栏和事物的方法viewDidLoad(从一些苹果的例子中复制而来)。

This is how I setup search bar and things in viewDidLoad (copied from some of apple's examples).

它在同一个viewcontroller中显示找到的结果,因为显示了未过滤的数据。它的表头中也有自己的搜索栏,直到需要它为止。

It presents the found results in same viewcontroller as your unfiltered data is shown. It also has its search bar in table header that is hidden until its needed.

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)];

这篇关于UISearchController搜索栏在活动时重叠第一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆