首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >激活时UISearchController搜索栏与第一个单元格重叠

激活时UISearchController搜索栏与第一个单元格重叠
EN

Stack Overflow用户
提问于 2017-07-05 23:01:01
回答 7查看 5.4K关注 0票数 3

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

然而,当我这样做并想要搜索时,我的活动搜索栏覆盖了第一个单元格的一部分。覆盖的零件具有与状态栏相同的高度。

我尝试了其他类似本文的文章和问题,但都没有帮助我解决它。然后我开始调整表视图头的大小。这是我做的

代码语言:javascript
代码运行次数:0
运行
复制
 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

  return 20.0f;
}

结果,当我点击搜索栏,开始搜索时,问题就不再存在了。

但是,当搜索栏不活动时,搜索栏和第一个单元格之间存在间隙

有什么办法解决这个问题吗?

编辑:添加self.automaticallyAdjustsScrollViewInsets = false;

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2017-07-06 03:05:58

这就是我在viewDidLoad中设置搜索栏和内容的方法(抄袭自苹果的一些例子)。

它将查找到的结果显示在与显示未过滤数据相同的视图控制器中。它还在表头中隐藏了搜索栏,直到需要时才会显示出来。

代码语言:javascript
代码运行次数:0
运行
复制
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, the 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)];
票数 3
EN

Stack Overflow用户

发布于 2017-07-06 01:04:53

我设法通过将RJiryes answer与滚动到顶部相结合的方式解决了这个问题。

代码语言:javascript
代码运行次数:0
运行
复制
-(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)];
}
票数 6
EN

Stack Overflow用户

发布于 2017-07-05 23:16:25

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

代码语言:javascript
代码运行次数:0
运行
复制
  self.tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)

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

代码语言:javascript
代码运行次数:0
运行
复制
  self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

这样,当空格处于非活动状态时,可以避免出现空格。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44929810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档