Setting background searchResultsTableView

I have a problem. I am trying to set the background of the searchResultsTableView. In my implementation of SearchViewController during viewDidLoad, I want to set the background either from my self.tableView or from self.searchDisplayController.searchResultsTableView. I'm doing it:

- (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 that, I have a tableView with my custom light yellow color, but when I click on the search bar and the table View has been replaced by searchDisplayController.searchResultsTableView, this results in white.

Thanks for any help.

+6
objective-c iphone
source share
4 answers

Use this method .. it is called when the search starts.

 - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView { tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"default.png"]]; } 

Screenshot of search results table view with custom background.

+21
source share

This i-Blue solution with didLoadSearchResultsTableView works just fine. However, I define the background color of the search result this way:

 - (void)viewWillAppear:(BOOL)animated { self.searchDisplayController. searchResultsTableView.backgroundColor = [UIColor blueColor]; [super viewWillAppear:animated]; } 

Still no problem.

+2
source share

I had the same problem when the background of the UITableViewStyleGrouped was white. This works for me (inspired by D-Griffin's answer, but added one line):

 - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView { tableView.backgroundView = nil; tableView.backgroundColor = color; } 
0
source share

I can't answer Matteo in the comments, so I do it here. In addition, it may be useful for future reference.

If you want to add a color that defines the RGB code, there is one simple trick.

The documentation states that the UIColor initWithRed: green: blue: alpha method accepts CGFLoat values ​​whose values ​​are between 0.0 and 1.0

Therefore, instead of executing UIColor(Red:255.0, green:252.0, blue:227.0, alpha:1.0) , which will not work, follow these steps:

 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 fine inside searchDisplayController (UISearchDisplayController: controller, didLoadSearchResultsTableView UITableView: tableView)

it's fast, but you can easily translate this to obj C

0
source share

All Articles