UISearchController dimsBackgroundDuringPresentation only when the search text is empty

I have a UISearchController and a UITableView. Code in viewDidLoad:

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; self.searchController.searchResultsUpdater = self; self.searchController.dimsBackgroundDuringPresentation = YES; [self.searchController.searchBar sizeToFit]; self.searchController.searchBar.delegate = self; self.searchController.delegate = self; self.tableView.tableHeaderView = self.searchController.searchBar; self.tableView.userInteractionEnabled = YES; 

I want the gray view to appear whenever I click on the search bar, and when I start typing, the gray view disappears and displays the tableView so that I can click on cells. That is, a gray view appears only when the search bar is empty (like the default search behavior in Mail and Contacts Apps). I tried to install

 self.searchController.dimsBackgroundDuringPresentation 

in the delegate method based on searchBar.text

 -(void )searchBarTextDidBeginEditing:(UISearchBar *)searchBar 

but that will not work. Any ideas?

Thanks,

+9
ios objective-c uisearchbar uisearchcontroller
source share
3 answers

self.searchController.dimsBackgroundDuringPresentation = YES is useful if you use a different view controller for searchResultsController. But in your code, you use the current view to display the results ( [[UISearchController alloc] initWithSearchResultsController:nil] ).

I want the gray view to appear whenever I click on the search bar, and when I start typing, the gray view disappears and displays the tableView so that I can click on cells.

This is the default behavior if you use a different view controller for the searchResultsController.

+4
source share

I added a subView for the table when the table is displayed, and sets gray and Alpha. when Dismiss SearchController removed the subview. I set the dim value to false. My code is how it can help you. I used the same table to show search results.

 // on header file UIView *dimView = null; //on .m file // create DimView for SearchControl - (void)showDimView { if(dimView == nil && self.searchController.active) { CGRect rcReplacementView = self.tableView.frame; dimView = [[UIView alloc] initWithFrame:rcReplacementView]; dimView.autoresizingMask = UIViewAutoresizingFlexibleWidth; dimView.backgroundColor = [UIColor blackColor]; dimView.alpha = 0.5; [self.view addSubview:dimView]; self.tableView.scrollEnabled = NO; //tap event for hide seachcontroll UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; [dimView addGestureRecognizer:singleFingerTap]; [singleFingerTap release]; } } //close SearchController if Tap on view - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer { if(searchController.searchBar.text.length <= 0) { [self.searchController setActive:NO]; } } // do something before the search controller is dismissed - (void)willDismissSearchController:(UISearchController *)searchController { if(dimView != nil) { [dimView removeFromSuperview]; dimView = nil; } self.tableView.scrollEnabled = YES; } 
+1
source share

One hacker approach is to set the background of the UISearchController to light gray when initializing the UISearchController and set dimsBackgroundDuringPresentation or obscuresBackgroundDuringPresentation to false , and then change the background in the textDidChange delegate method to clear it.

 (void)viewDidLoad() { self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; self.searchController.searchResultsUpdater = self; self.searchController.searchBar.placeholder = NSLocalizedString(@"Search", @""); self.searchController.view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.2f]; self.searchController.dimsBackgroundDuringPresentation = false; [searchController.searchBar sizeToFit]; } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { NSUInteger length = [searchText length]; if (length > 0) { self.searchController.view.backgroundColor = [UIColor clearColor]; [self.searchController.view reloadInputViews]; } else { self.searchController.view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.2f]; [self.searchController.view reloadInputViews]; } } 
0
source share

All Articles