UISearchController dimsBackgroundDuringPresentation only when the search text empty
Asked Answered
T

3

10

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,

Thundershower answered 18/11, 2015 at 19:47 Comment(8)
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.Zito
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?Thundershower
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-implementationZito
@Zito Yes I followed the tutorial and it worked. Thanks.Thundershower
I'm glad that worked out for you.Zito
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.Tonality
@Thundershower I am also facing this issue, can you please help me out, I checked the tutorial link but it's not opening.Exoskeleton
@ParthBarot I hope this helps: raywenderlich.com/…Thundershower
L
4

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.

Lexicologist answered 29/6, 2016 at 12:17 Comment(1)
I think Apple should disable this weird and useless effect through exception or may be a warning in the log... I mean if you choose searchResultsController as current (nil), then dimsBackgroundDuringPresentation should never be YES.Mentality
T
1

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;
}
Trilogy answered 20/11, 2015 at 8:53 Comment(1)
Thank you, I prefer to implement the standard implementation from apple.Thundershower
P
0

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

}
Perpetual answered 2/9, 2019 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.