Search code examples
iphoneobjective-ciosuisearchdisplaycontroller

UISearchDisplayController hide drop shadow


If you look closely to the bottom of the UISearchBar in a UISearchDisplayController, you'll notice it has a subtile drop shadow. This shadow doesn't fit in the design of the app I'm currently working on, so I'm trying to remove/hide it. Unfortunately I have not yet succeeded.

During my research into this drop shadow, I found that it's not part of the UISearchBar. When I remove the UISearchDisplayController's UISearchBar from its superview in - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller, the shadow remains visible.

The shadow turned out to be part of the UISearchDisplayController's searchResultsTableView: when I hide the searchResultsTableView, the shadow disappears. However, I have not been able to trace down the view that has the shadow on its layer. I tried recursively iterating through all visible views (starting at [[UIApplication sharedApplication] window]) and then hiding the drop shadow of each view and setting its clipsToBounds property to YES, which also did not yield the desired result.

Any ideas?


Solution

  • I finally found a solution. Since setting clipsToBounds to YES and hiding the drop shadow of each view in the hierarchy didn't work, it's obvious that the shadow is an image. After iterating through all subviews of the searchResultsTableView and printing their class property, I found an instance of _UISearchBarShadowView, which obviously is the culprit. So what I'm doing now is finding the _UISearchBarShadowView and setting its alpha to 0.0f.

    - (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
    {
        [self _findAndHideSearchBarShadowInView:tableView];
    }
    
    - (void)_findAndHideSearchBarShadowInView:(UIView *)view
    {
        NSString *usc = @"_";
        NSString *sb = @"UISearchBar";
        NSString *sv = @"ShadowView";
        NSString *s = [[usc stringByAppendingString:sb] stringByAppendingString:sv];
    
        for (UIView *v in view.subviews)
        {
           if ([v isKindOfClass:NSClassFromString(s)]) {
                v.alpha = 0.0f;
            }
            [self _findAndHideSearchBarShadowInView:v];
        }
    }