我有一个搜索栏(SearchBar),我将其设置在一个表视图控制器(TableViewController)中。我参考了这个类似的问题UISearchBar cannot become first responder after UITableView did re-appear,但是我仍然无法将其设置为第一响应者。 .h文件中:
@property (strong, nonatomic) UISearchController *searchController;
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
在viewDidLoad方法中:
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.tableView.tableHeaderView = self.searchController.searchBar;
并且在viewDidAppear方法中:
-(void)viewDidAppear:(BOOL)animated {
[self.searchController setActive:YES];
[self.searchController.searchBar becomeFirstResponder];
[super viewDidAppear:animated];
}
当我切换到搜索视图时,搜索栏会发生动画,但键盘没有弹出。
dispatch_after()
稍微延迟becomeFirstResponder
方法会发生什么? - diederikhsuper viewDidAppear
之前调用它,有什么原因吗? - CrimsonChris