我正在使用UISearchController
在我的表视图中搜索数据。我没有使用表视图控制器。当我的搜索栏处于活动状态时,我想隐藏导航栏,因此我将self.searchController.hidesNavigationBarDuringPresentation = YES;
设置为yes。
然而,当我这样做并想要搜索时,我的活动搜索栏覆盖了第一个单元格的一部分。覆盖的零件具有与状态栏相同的高度。
我尝试了其他类似本文的文章和问题,但都没有帮助我解决它。然后我开始调整表视图头的大小。这是我做的
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20.0f;
}
结果,当我点击搜索栏,开始搜索时,问题就不再存在了。
但是,当搜索栏不活动时,搜索栏和第一个单元格之间存在间隙
有什么办法解决这个问题吗?
编辑:添加self.automaticallyAdjustsScrollViewInsets = false;
后
发布于 2017-07-05 19:05:58
这就是我在viewDidLoad中设置搜索栏和内容的方法(抄袭自苹果的一些例子)。
它将查找到的结果显示在与显示未过滤数据相同的视图控制器中。它还在表头中隐藏了搜索栏,直到需要时才会显示出来。
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)];
发布于 2017-07-05 17:04:53
我设法通过将RJiryes answer与滚动到顶部相结合的方式解决了这个问题。
-(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)];
}
发布于 2017-07-05 15:16:25
您可以从顶部添加内容插入,而不是添加标题。
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)
这样,当空格处于非活动状态时,可以避免出现空格。
https://stackoverflow.com/questions/44929810
复制相似问题