Reputation: 145
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,
Upvotes: 10
Views: 6070
Reputation: 698
One hacky approach is to set the UISearchController background to light grey colour while initialising the UISearchController and set the dimsBackgroundDuringPresentation or obscuresBackgroundDuringPresentation to false, and then in the textDidChange delegate method change the background to clear.
(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];
}
}
Upvotes: 0
Reputation: 22930
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.
Upvotes: 4
Reputation: 3605
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;
}
Upvotes: 1