【问题标题】:UISearchController's UISearchBar embedded in Navigation Bar: Does not become first responder嵌入导航栏中的 UISearchController 的 UISearchBar:不会成为第一响应者
【发布时间】: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


【解决方案1】:

尝试实现UISearchBarDelegate

并在searchBarTextDidBeginEditing 中调用searchBarbecomeFirstResponder()

然后在searchBarTextDidEndEditing调用resignFirstResponder()

【讨论】:

  • 试过了,但是委托方法甚至没有被调用。
  • 您是否同时遵守 UISearchBarDelegate 和 UISearchControllerDelegate 协议? :S
  • 对这两种协议和所有协议的每一种组合都是。它不会改变
【解决方案2】:

老问题,但刚刚遇到了类似的问题。确保在 VC heirachy 中的有效导航控制器中嵌入了“searchBar”。那恰好是我的错误。所以这行得通;

CCCTestVC *tVC = [[CCCTestVC alloc] init];
UINavigationController *nC = [[UINavigationController alloc] initWithRootViewController:tVC];
[self.navigationController  presentViewController:nC animated:YES completion:nil];

而不是这样做;

CCCTestVC *tVC = [[CCCTestVC alloc] init];
[self.navigationController pushViewController:tVC animated:YES];

【讨论】:

    猜你喜欢
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多