Search code examples
iosiphoneobjective-cinterface-builderxcode5

UISearchDisplayController weird behavior (video)


When I add an UISearchDisplayController in IB (Xcode 5) to the UIViewController that is displayed in a UINavigationController and press on the search bar, the search bar gets overlaid by a black translucent view.

enter image description here

Here is the video: http://quick.as/ezrc7bq

I haven't yet touched any code, so I am not sure what is going on there.


Solution

  • This is actually a bug in iOS UISearchBarDisplayController (when used within UINavigationController). The default behaviour is that to highlight textbox the remaining view becomes dim but this dim view frame does not consider navigation bar height into account.

    Easiest way to get around this problem is to implement searchbar delegate

    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
    {
        self.navigationController.navigationBarHidden = YES;
    }
    
    - (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller{
        self.navigationController.navigationBarHidden = NO;
    }
    

    other alternatives include to modify the frame of search bar

    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
    {
        // animate the search bar to the top ie. y=0
        [UIView animateWithDuration:0.25f animations:^{
            CGRect frame = controller.searchBar.frame;
            frame.origin.y = 0;
            controller.searchBar.frame = frame;
        }];
    
    }