Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a custom background to UISearchDisplayController's table view?

Tags:

uikit

iphone

I want to add a custom UIImageView to UISearchDisplayController's table view background and set table view's background color to clearColor. Tried a few different approach but couldn't find the right solution. Any idea how to approach this?

Note: I don't want to add to searchDisplayController's searchResultsTableView's view hierarchy, but rather overlay another sibling view below it)

like image 741
Boon Avatar asked Oct 24 '09 06:10

Boon


2 Answers

You can set the background image in a similar way you would for your main table, only set it in the searchDisplayControllerDidBeginSearch delegate method. For instance:-

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
[controller.searchResultsTableView setDelegate:self];
UIImageView *anImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradientBackground.png"]];
controller.searchResultsTableView.backgroundView = anImage;
[anImage release];
controller.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
controller.searchResultsTableView.backgroundColor = [UIColor clearColor]; }
like image 118
AJ. Avatar answered Oct 31 '22 16:10

AJ.


You can also do this wherever you instantiate your UISearchDisplayController. In my app I was doing this in my UITableView viewDidLoad method and was matching the styles between the two tables:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.separatorColor = [UIColor blackColor];
    self.tableView.backgroundColor = [UIColor grayColor];
    self.tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchController.delegate = self;
    searchController.searchResultsDataSource = self;
    searchController.searchResultsDelegate = self;

    searchController.searchResultsTableView.separatorColor = self.tableView.separatorColor;
    searchController.searchResultsTableView.backgroundColor = self.tableView.backgroundColor;
    searchController.searchResultsTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
}
like image 38
theTRON Avatar answered Oct 31 '22 17:10

theTRON