似乎是一个常见的iOS bug,但我尝试了所有能找到的解决方法,却没有任何结果。
我正在创建一个UISearchController并将其添加到UIView包装器中,代码如下:
每当触摸搜索栏时,键盘弹出,但整个搜索栏都会消失,这是一个问题。
我查看了以下帖子:
我正在创建一个UISearchController并将其添加到UIView包装器中,代码如下:
self.searchController = [[SearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
[self.searchBarWrapper addSubview:self.searchController.searchBar];
[self.searchController.searchBar sizeToFit];
每当触摸搜索栏时,键盘弹出,但整个搜索栏都会消失,这是一个问题。
我查看了以下帖子:
- 当我开始搜索时,我的UISearchController中的UISearchBar消失了。为什么?
- iOS 9中,当UISearchController处于活动状态时,searchBar从表头视图中消失
- UISearchController searchBar在第一次点击后消失
- 位于UINavigationController中的TableViewController中的消失的UISearchController
- 当UISearchController处于活动状态并选择一个新标签时,UITableView消失了
- 在点击后Search Bar消失了
- UISearchController中的searchBar在tableHeaderView中动画消失了
- 在iOS UISearchController中消失的搜索栏
- 在输入时Search Bar从视图中消失了
我尝试了各种解决方案,最终添加了这个初始化代码:
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = NO;
self.navigationController.definesPresentationContext = YES;
self.navigationController.extendedLayoutIncludesOpaqueBars = YES;
self.extendedLayoutIncludesOpaqueBars = YES;
这些委托函数
- (void)willPresentSearchController:(UISearchController *)searchController {
// definitely runs, if I put a breakpoint here it stops
self.navigationController.navigationBar.translucent = YES;
[self.searchBarWrapper addSubview:self.searchController.searchBar];
searchController.searchResultsController.view.hidden = NO;
}
-(void)willDismissSearchController:(UISearchController *)searchController{
self.navigationController.navigationBar.translucent = NO;
searchController.searchResultsController.view.hidden = NO;
}
但是当触摸时,搜索栏仍然会消失。我还能尝试什么?
我认为我的代码与其他帖子的区别在于我使用了通过编程方式创建的UIViewController
而没有使用XIB。有一个UITableView
子视图,但它不直接与搜索栏交互。没有UINavigationBar
,搜索栏位于视图顶部。
更新:
这是相对于搜索栏的我的视图层次结构:
- UIViewController
- UIView
- UIScrollView
- UIViewController
- UIView
- UISearchBar