ios UIsearchcontroller 中的搜索栏消失

Posted

技术标签:

【中文标题】ios UIsearchcontroller 中的搜索栏消失【英文标题】:search bar getting disappeared in ios UIsearchcontroller 【发布时间】:2015-11-03 08:11:50 【问题描述】:

我正在使用UISearchController,当我单击搜索栏时,我会看到动画,好像搜索栏将转到所呈现的搜索控制器的导航栏,但动画消失了,而呈现的键盘则停留在那里。

在viewcontroller.m中,viewdidload方法,相关部分:

UIView *searchView = [[UIView alloc]initWithFrame:CGRectZero];
searchResultsViewController =[[SearchResultsViewController alloc]initWithNibName:@"SearchResultsViewController" bundle:nil];
self.searchController =[[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater=self;
self.searchController.dimsBackgroundDuringPresentation = NO;
searchView = self.searchController.searchBar;
self.definesPresentationContext = YES;
self.searchController.searchBar.delegate=self;
searchView.frame =CGRectMake(0, 50, [[UIScreen mainScreen]bounds].size.width, 40);
[self.view addSubview:searchView];

SearchResultsViewController 是一个带有过滤数组的UIViewController,它的值来自 ViewController 中的-(void)updateSearchResultsForSearchController:(UISearchController *)searchController 方法

由于搜索栏消失,我尝试将搜索栏作为 ViewController 中 TableView 的标题,它工作正常。但是当我只是将搜索栏视图作为自定义创建的视图而不是表头时,它正在消失。

任何见解将不胜感激。谢谢

【问题讨论】:

【参考方案1】:

尝试以下两种设置:

self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = NO;

【讨论】:

还是一样。 你能检查一下this SO Thread是否对你有帮助吗? @vivek 如果答案没有解决您的问题,您为什么要接受?

以上是关于ios UIsearchcontroller 中的搜索栏消失的主要内容,如果未能解决你的问题,请参考以下文章

NavigationItem iOS 11 Apple 方式中的 UISearchController

iOS中的2中搜索方式UISearchDisplayController和UISearchController

UISearchController 中的取消按钮在 iOS 13 中没有正确消失

IOS8 UISearchController 直接显示搜索栏

同时实现 UISearchController 和 UISearchDisplayController

如何在 iOS 8 中使用 UISearchController,其中 UISearchBar 在我的导航栏中并且有范围按钮?