【发布时间】:2017-07-13 18:24:29
【问题描述】:
我正在尝试在导航控制器栏的 titleView 中嵌入 UISearchController 的搜索栏。确切地说,这可以在 Apple 的 UICatalog 示例代码中看到并且有效:
// Create the search results view controller and use it for the UISearchController.
AAPLSearchResultsViewController *searchResultsController = [self.storyboard instantiateViewControllerWithIdentifier:AAPLSearchResultsViewControllerStoryboardIdentifier];
// Create the search controller and make it perform the results updating.
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = searchResultsController;
self.searchController.hidesNavigationBarDuringPresentation = NO;
/*
Configure the search controller's search bar. For more information on how to configure
search bars, see the "Search Bar" group under "Search".
*/
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchController.searchBar.placeholder = NSLocalizedString(@"Search", nil);
// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar;
self.definesPresentationContext = YES;
我尝试对这段代码做同样的事情:
self.searchSuggestionsController = [[SearchSuggestionsTableViewController alloc]initWithStyle:UITableViewStylePlain];
self.searchSuggestionsController.delegate = self;
self.searchController = [[UISearchController alloc]initWithSearchResultsController:self.searchSuggestionsController];
self.searchController.delegate = self;
self.searchController.searchResultsUpdater = self.searchSuggestionsController;
self.searchController.searchBar.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = YES;
[self.searchController setHidesNavigationBarDuringPresentation:NO];
self.searchController.searchBar.searchBarStyle = UISearchBarStyleProminent;
[self.searchController.searchBar setPlaceholder:NSLocalizedString(@"Search", nil)];
[self.navigationItem setTitleView:self.searchController.searchBar];
if (self.searchQuery) {
[self.searchController.searchBar setText:_searchQuery.queryString];
}
self.definesPresentationContext = YES;
但是当我在设备或模拟器中运行它时,导航栏中的搜索栏永远不会成为第一响应者。键盘不显示,我无法输入文字。我能做的是使用清除按钮,文本会清除,但我仍然无法输入任何文本。
有什么想法吗?
【问题讨论】:
-
顺便说一句:更改导航栏样式没有帮助
-
你找到答案了吗?
-
哇,我解决了)
标签: ios uinavigationcontroller uisearchbar uisearchcontroller