UISearchBar lost first responder after reloading UICollectionView data

I have a collection view in my iPad app and you want to add a search function. The collection browsing data source is nsfetchedresultcontroller.

I placed the UISearchBar in the UIToolBar located at the bottom of the window. When the user enters the search bar, I animated the movement of the entire toolbar along with the appearance of the keyboard to keep the toolbar (and therefore the search bar) visible. When the user enters a search string, I performed the following method:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if (searchText && searchText.length){ NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", searchText]; [NSFetchedResultsController deleteCacheWithName:nil]; [self.fetchedResultsController.fetchRequest setPredicate:searchPredicate]; }else{ [NSFetchedResultsController deleteCacheWithName:nil]; [self.fetchedResultsController.fetchRequest setPredicate:nil]; } [self performFetch]; } 

My problem is that when the predicate does filter the data set, the toolbar located above the keyboard returns to its original position. If, for example, what the user entered does not change the data set, the toolbar remains in place.

How can I keep the toolbar in place during user input and ultimately change the collection view?

+4
source share

Source: https://habr.com/ru/post/1469030/


All Articles