My beautiful dark table background had become styled back to a white background when working with the UISearchController and typing a word to filter. Lovely, I thought. It looks like it creates a new tableview for the filtering and isn’t smart enough to carry over styles. Why not just look at Apple’s documentation to see if I can find a way to reference the table view of filtered content with my UISearchController.

Screen Shot 2016-06-29 at 11.50.50 PM

 

faith

 

You can only really reference the search bar. And the interfaces (UISearchControllerDelegateUISearchResultsUpdatingUISearchBarDelegate) will not help as there will never be a reference to this table supplied. So much for getting a simple table filter and background color working. Without digging for an answer at least.

A Workaround

This ‘temporary’ table EXISTS so we can get a reference to by iterating through the view structure. In the debug window this works with a simple [self.view recursiveDescription]. Here you can see the ‘root’ tableview for my UITableViewController screen.

<UITableView>
   | <UIImageView>
   | <UITableViewWrapperView>
   |    | <UITableViewCell>
   |    |    | <UITableViewCellContentView>
   |    |    | <_UITableViewCellSeparatorView>
   |    |    | <UIButton>
   |    |    |    | <UIImageView>
   |    |    | <_UITableViewCellSeparatorView>
   | <UIImageView>
   | <UIImageView>
   | <UISearchDisplayControllerContainerView>
   |    | <UIView>
   |    |    | <UISearchResultsTableView>
   |    |    |    | <UITableViewWrapperView>
   |    |    |    | <_UITableViewCellSeparatorView>
   |    |    |    | <_UITableViewCellSeparatorView>
   |    |    |    | <UIImageView>
   |    |    |    | <UIImageView>
   |    |    |    | <_UISearchBarShadowView>
   |    | <UIView>
   |    |    | <UISearchBar>
   |    |    |    | <UIView>
   |    |    |    |    | <UISearchBarBackground>
   |    |    |    |    | <UISearchBarTextField>
   |    |    |    |    |    | <_UISearchBarSearchFieldBackgroundView>
   |    |    |    |    |    | <UIFieldEditor>
   |    |    |    |    |    |    | <_UIFieldEditorContentView>
   |    |    |    |    |    |    |    | <UITextSelectionView>
   |    |    |    |    |    |    |    |    | <UIView>
   |    |    |    |    |    | <UIButton>
   |    |    |    |    |    | <UIImageView>
   |    |    |    |    | <UINavigationButton>
   |    |    |    |    |    | <UIButtonLabel>
   |    | <UIView>
   |    |    | <_UISearchDisplayControllerDimmingView>

Notice how the main tableview has an embedded sub view UISearchResultsTableView. We simply need to reference that. Heres a quick and easy snippet which must be called after it exists (this table will not exist until a filter has been executed.

-(void)adjustBackgroundOnSecondaryTable:(UIView*)baseView{
    //Simple boolean to ensure this fix only gets run once.
    if(m_backgroundFixed)
        return;
    
    //Find that secondary table...
    for (UIView *subview in baseView.subviews){
        if([subview isKindOfClass:[NSClassFromString(@"UISearchResultsTableView") class]])
        {
            //HERE IS THE SECONDARY TABLE! You can work with it as UIView here or cast it to a Table!
            [subview setBackgroundColor:[UIColor clearColor]];
            m_backgroundFixed = YES;
        }
        else{
            //Continue search 
            [self adjustBackgroundOnSecondaryTable:subview];
        }
    }
}

To make sure the table exists before trying to modify it I executed the adjustment during a textDidChange from the search bar:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    //Your filter code here
    //...

    //Ensure that background color is fixed
    [self adjustBackgroundOnSecondaryTable:self.view];
}

XCODE. Where changing a table background can sometimes take an hour of fussing!

No Comments

There are no comments related to this article.