[iOS problem highlights] UISearchController.searchBar becomeFirstResponder does not take effect

Problem description

UISearchController iOS provides the latest to facilitate the realization of the search component box and search results page, but we often need to access this page when you let the search box focus and pop-up keyboard, provide a better experience to the user

Solution

Various Google stackoverflow followed by the following code to achieve the desired effect (I was put on the searchBar navigationBar):

- (void) viewDidAppear: (BOOL) animated [super viewDidAppear: {animated]; self.searchController.active = true;} #pragma mark UISearchControllerDelegate (void) didPresentSearchController: (UISearchController * searchController) {becomeFirstResponder]} [self.searchController.searchBar

View the document, we can easily understand the principle of doing so

Note that: must be in viewDidAppear active searchController

Test iOS 8 no problem, but iOS 9 can not pop up the keyboard, but also a search, the code will be amended as follows:

- (void) viewDidAppear: (BOOL) animated [super viewDidAppear: {animated]; self.searchController.active = true;} #pragma mark UISearchControllerDelegate (void) didPresentSearchController: (UISearchController *) searchController animateWithDuration:0.1 animations:^{} completion:^ {[UIView (BOOL finished) {[self.searchController.searchBar}}]; becomeFirstResponder];

Analysis of the reasons, didPresentSearchController should be in searchController present after the completion of the call, but the fact is not the end of estimates call, so there becomeFirstResponder no effect, solve the problem of delay (not necessarily cause)

Effect drawing

[iOS problem highlights] UISearchController.searchBar becomeFirstResponder does not take effect
Paste_Image.png