Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController dimsBackgroundDuringPresentation only when the search text empty

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,

like image 240
Missa Avatar asked Nov 18 '15 19:11

Missa


2 Answers

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.

like image 108
Parag Bafna Avatar answered Nov 07 '22 16:11

Parag Bafna


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;
}
like image 29
Sabareesh Avatar answered Nov 07 '22 15:11

Sabareesh