I have UISearchController and a UITableView. The Code in viewDidLoad is:
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 grey view to be appeared whenever I tap on the search bar and when I start typing, the grey view disappears and shows the tableView so I can tap on the cells. Thats mean the grey view appears only when the search bar is empty (just like the default search behavior in Mail and Contacts Apps). I tried to set the
self.searchController.dimsBackgroundDuringPresentation
in the delegate method based on the searchBar.text
-(void )searchBarTextDidBeginEditing:(UISearchBar *)searchBar
but it does not work. Any ideas?
Thanks,
self.searchController.dimsBackgroundDuringPresentation = YES
is useful if you are using another view controller for searchResultsController But in your code you are using current view to show the results ([[UISearchController alloc] initWithSearchResultsController:nil]
).
I want the grey view to be appeared whenever I tap on the search bar and when I start typing, the grey view disappears and shows the tableView so I can tap on the cells.
This is default behaviour if you are using another view controller for searchResultsController.
I added subView for table when table show and set gray color and Alpha. when Dismiss SearchController removed subview. I set dim property as false. My Code as bellow it may help you. I have used same table for showing Search Result.
// 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With