10
votes

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,

3
That is how dimsBackgroundDuringPresentation works. How are you implementing uisearchcontroller? It will only not dim search results. Seems like you are likely not presenting any.beyowulf
Thats all what I did for the UISearchController. everything else is implemented by the table view and (void)updateSearchResultsForSearchController:(UISearchController *)searchController reloads the tabla view based on the search text. is there any other thing I need to do?Missa
Unfortunately it's more complicated than that. You need to search your tableview's data source and find search results then present them to the user. You can find a tutorial on how to do that here: jhof.me/simple-uisearchcontroller-implementationbeyowulf
@beyowulf Yes I followed the tutorial and it worked. Thanks.Missa
But there should be way to set dimsBackgroundDuringPresentation property dynamically, For you it was easy to write new table, but if table is complex many developer prefers to use the same controller to display results, what to do in such case ? Problem is still there.Mayur Kothawade

3 Answers

4
votes

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.

1
votes

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;
}
0
votes

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];
    }

}