最近我从使用已弃用的IB中的UISearchDisplayController和UISearchBar切换到使用iOS8代码中的UISearchController。
但是我遇到的问题是,尽管焦点被正确地分配(您可以看到加载视图后取消按钮会动画显示在搜索栏的右侧),但键盘并没有显示出来。
下面是我的代码。
.h
@property (nonatomic, strong) UISearchController *searchController;
.m
- (void)viewDidLoad {
[super viewDidLoad];
...
[self initializeSearchController];
....
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.searchController setActive:YES];
[self.searchController.searchBar becomeFirstResponder];
}
- (void)initializeSearchController {
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
[self.searchController.searchBar sizeToFit];
[self.tableView setTableHeaderView:self.searchController.searchBar];
self.definesPresentationContext = YES;
}
我目前尝试过的方法:
按照另一个SO帖子的建议,在0.2秒的延迟后调用了becomeFirstResponder。
我在viewDidAppear中设置了断点,并验证了self.searchController和self.searchController.searchBar都是有效的对象,都不为nil。
我尝试遵循UISearchControllerDelegate并使用以下代码片段:
这里:
- (void)didPresentSearchController:(UISearchController *)searchController {
//no matter what code I put in here to becomeFirstResponder, it doesn't
//matter because this is never called, despite setting the
//self.searchController.delegate = self AND
//self.searchController.searchBar.delegate = self.
}
我从storyboards中从头创建了一个新的视图,并进行了跳转,以确保我的视图中没有旧的searchBar残留。但这也没有起作用。
我只在真实设备(iPhone 6)上测试过,这不是模拟器无法显示键盘的问题。
我已经没有更多的想法了,并且我已经看过与此相关的每个问题和答案。什么都没用。
再次澄清一下,searchBar正确地成为第一个响应者,证明了右侧的取消按钮正在屏幕上动画显示,但是键盘没有出现,搜索栏中的光标也没有闪烁。