Setting background of searchResultsTableView
I've a开发者_StackOverflow中文版 problem. I trying to set the background of an searchResultsTableView. Into my SearchViewController implementation, during the viewDidLoad, I want to set the background either of my self.tableView and of self.searchDisplayController.searchResultsTableView. I do this:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Search";
UIColor *customColor = [UIColor colorWithRed:255.0 green:252.0 blue:227.0 alpha:1.0];
self.tableView.backgroundColor = customColor;
self.searchDisplayController.searchResultsTableView.backgroundColor = customColor;
}
After this, I've the tableView with my custom light-yellow color but, when I tap the search bar, and the tableView was replaced by searchDisplayController.searchResultsTableView, this results white.
Thanks for any help.
Use this method .. it will call when you start searching..
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"default.png"]];
}
That i-Blue's solution with didLoadSearchResultsTableView works just fine. However I define search result background color this way:
- (void)viewWillAppear:(BOOL)animated
{
self.searchDisplayController.
searchResultsTableView.backgroundColor =
[UIColor blueColor];
[super viewWillAppear:animated];
}
So far no problems.
I had the same problem with UITableViewStyleGrouped-type background was white. This works for me (inspired by D-Griffin answer but added one line):
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
tableView.backgroundView = nil;
tableView.backgroundColor = color;
}
I can't reply to Matteo in comments, so I'm doing it here. Besides, it might be useful for further reference.
If you want to add a color specifying the RGB code, there's one simple trick.
The documentation says the UIColor method initWithRed:green:blue:alpha accepts CGFLoat values, which are values between 0.0 and 1.0
so Instead of doing UIColor(Red:255.0, green:252.0, blue:227.0, alpha:1.0)
, which won't work, do the following :
let red:CGFLoat = 255/255
let green:CGFloat = 252/255
let blue:CGFloat = 227/255
UIColor(red:red, green:green, blue:blue,alpha:1.0)
this works perfectly well inside the searchDisplayController(UISearchDisplayController:controller, didLoadSearchResultsTableView UITableView:tableView)
this is swift, but you could translate this in obj C easily enough
精彩评论