【发布时间】:2016-08-19 08:57:32
【问题描述】:
在navigationItem titleView中设置UISearchController搜索栏时,搜索栏无法编辑。
在我的 viewDidLoad 中,我正在配置 UISearchController。
self.searchViewController = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([SearchViewController class])];
self.searchViewController.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchViewController];
self.searchController.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
self.navigationItem.titleView = self.searchController.searchBar;
我无法点击搜索栏。光标没有出现,也没有用户交互。
奇怪的是,如果我在本地初始化 UISearchController 而不将其设置为属性,那么我可以编辑搜索栏,只是没有委托回调。
self.searchViewController = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([SearchViewController class])];
self.searchViewController.delegate = self;
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchViewController];
searchController.delegate = self;
searchController.searchResultsUpdater = self;
searchController.searchBar.delegate = self;
self.navigationItem.titleView = searchController.searchBar;
另一个有趣的行为是清除按钮有效(如果在初始化时在搜索栏中设置了一些文本)。
【问题讨论】:
-
我有同样的问题,建议的答案都不起作用。你找到解决方案了吗?
-
@SilviaHisham 是的,对我有用的是我下面的答案stackoverflow.com/a/36893699/4099344
标签: ios objective-c cocoa-touch uisearchcontroller