1

(I know this may seem as duplicate, but I'm telling you all nothing I've found here worked)

I have a UITableView with a search display controller and a search bar int it, I'm trying to set the tintColor according to the company colors. But it's translucent no matter what I try.

Here is the sample code:

[self.searchDisplayController.searchBar setTranslucent:NO];
[self.searchDisplayController.searchBar setBarTintColor:[[ConfToolbox sharedInstance] getBarTintColor]];

[[ConfToolbox sharedInstance] getBarTintColor] returns a dark-ish blue UIColor .

I've looked around for an answer but nothing worked, even this accepted answer.

Any help would be great.

Cheers,

Ayu.

Community
  • 1
  • 1
Ayu
  • 926
  • 1
  • 9
  • 15

2 Answers2

4

I achieved what I wanted using this, just in case someone faces the same problem:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(10,10), YES, 0);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[ConfToolbox sharedInstance] barTintColor].CGColor);
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0,0,10,10));
UIImage* coloredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.searchDisplayController.searchBar setBackgroundImage:coloredImage forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

You can set the second parameter of CGContextSetFillColorWithColor with whatever color you want.

From the documentation

The default value is YES. If the search bar has a custom background image, the default is YES if any pixel of the image has an alpha value of less than 1.0, and NO otherwise.

Since the image is made from a color with an alpha value of 1, the translucent effect is off as stated.

Ayu
  • 926
  • 1
  • 9
  • 15
0

try like this,

    self.searchBar.frame = CGRectMake(self.searchBar.frame.origin.x, self.searchBar.frame.origin.y+7, self.searchBar.frame.size.width, 30);
    self.searchBar.placeholder = @"Search...";
    self.searchBar.layer.cornerRadius = 15;
    self.searchBar.layer.backgroundColor = [UIColor redColor].CGColor;
    self.searchBar.layer.borderWidth=8;
    [[NSClassFromString(@"UISearchBarTextField") appearanceWhenContainedIn:[UISearchBar class], nil] setBorderStyle:UITextBorderStyleNone];
    self.searchBar.layer.borderColor=[UIColor redColor].CGColor;
Balu
  • 8,470
  • 2
  • 24
  • 41
  • This just add a sort of border around the searchbar with an opaque red color, it didn't change the search bar's tint color itself – Ayu Jun 23 '14 at 10:56