Search code examples
iphoneiosuisearchbaruisearchdisplaycontroller

How does Mail app disable UISearchBar when in edit mode?


Using a SearchDisplayController and couldn't find any methods that would accomplish this?


Solution

  • Check out the UISearchDisplayDelegate and the UISearchBarDelegate for knowing when the search bar is in edit mode.

    Here is some code to add an overlay over the searchBar, which I've adapted from this example,

    In your .h file,

    @interface ...
    {
        UIView *disableViewOverlay;
        ...
    }
    
    @property(retain) UIView *disableViewOverlay;
    

    And in the .m file,

    @synthesize disableViewOverlay;
    
    ...
    
    -(void) disableSearchBar 
    {
        self.disableViewOverlay = [[UIView alloc]
                               initWithFrame:CGRectMake(0.0f,0.0f,320.0f,44.0f)];
        self.disableViewOverlay.backgroundColor=[UIColor blackColor];
        self.disableViewOverlay.alpha = 0;
    
        self.disableViewOverlay.alpha = 0;
        [self.view addSubview:self.disableViewOverlay];
    
        [UIView beginAnimations:@"FadeIn" context:nil];
        [UIView setAnimationDuration:0.5];
        self.disableViewOverlay.alpha = 0.4;
        [UIView commitAnimations];
    }
    
    -(void) enableSearchBar 
    {
        [disableViewOverlay removeFromSuperview];
        [searchBar resignFirstResponder];
    }
    

    You can adjust the alpha values until it appears the way you want it to.

    Hope this helps!