【发布时间】:2016-07-27 08:19:42
【问题描述】:
似乎是一个常见的 iOS 错误,但我已经尝试了所有我能找到的解决方案,但没有看到任何结果。
我正在创建一个 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];
问题是每当触摸搜索栏时,键盘会弹出,但整个搜索栏消失了。
我看了这些帖子:
- My UISearchBar in UISearchController disappear when I start searching. Why?
- iOS 9 searchBar disappears from table header view when UISearchController is active
- UISearchController searchBar disappears on first click
- Disappearing UISearchController in a TableViewController that is in a UINavigationController
- UITableView disappears when UISearchController is active and a new tab is selected
- Search Bar disappears after tap on it
- UISearchController searchBar in tableHeaderView animating out of the screen
- search bar getting disappeared in ios UIsearchcontroller
- Search Bar disappeared from view while typing
并尝试了所有解决方案,最终添加了这个初始化代码:
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;
}
但触摸时搜索栏仍然消失。我还能尝试什么?
我认为我的代码与其他帖子之间的区别在于我使用的是在没有 XIB 的情况下以编程方式创建的 UIViewController。有一个UITableView 子视图,但它不直接与搜索栏交互。没有UINavigationBar,搜索栏在视图顶部。
更新:
这是我相对于搜索栏的视图层次结构:
- UIViewController
- UIView
- UIScrollView
- UIViewController
- UIView
- UISearchBar
【问题讨论】:
标签: ios objective-c ios9 uisearchcontroller