UISearchBar in a UIView Showing a White Border and Not Correctly Applying clearColor for tintColor and barTintColor

StackOverflow https://stackoverflow.com/questions/22758881

Question

I have a UITableView and a UISearchBar embedded within a UIView. The searchBar is positioned at the top and I am trying to achieve something which seems to be quite straight forward; I am trying to make the UISearchBar be transparent and take the colour of the background.

Here's an example image.

enter image description here

I have tried quite a number of things to no success.

I set the barTint and the tintColor to clear:

self.nameAddSearchBar.barTintColor = [UIColor clearColor]; 
self.nameAddSearchBar.tintColor = [UIColor clearColor]; 

I have tried setting the barMetrics:

[self.nameAddSearchBar setBackgroundImage:[UIImage imageNamed:@"Transparent.png"] forBarPosition:0 barMetrics:UIBarMetricsDefault];

I tried just

self.nameAddSearchBar.backgroundImage = [UIImage imageNamed:@"Transparent.png"]; 

But none of these are working. I can add a specific NavBar image I have:

self.nameAddSearchBar.backgroundImage = [UIImage imageNamed:@"GreenNav.png"]; 

That works perfectly but it looks odd because of the break in the image. Therefore, I just want this UISearchBar to be a clearColor so the background colour works through it.

The UISearchBar is created in the storyboard and I have Translucent turned on. I have the bar tint, the background and the tint set to Clear colour, but in all cases, it's always with the white border in the image.

Any guidance on this would be really appreciated.

Was it helpful?

Solution

enter image description here try like this,

    self.searchBar.backgroundColor = [UIColor clearColor];
    self.searchBar.searchBarStyle = UIBarStyleBlackTranslucent;
    self.searchBar.placeholder = @"Search...";
    self.searchBar.layer.cornerRadius = 15;
    self.searchBar.layer.backgroundColor = [UIColor clearColor].CGColor;
    self.searchBar.layer.borderWidth=8;
   [[NSClassFromString(@"UISearchBarTextField") appearanceWhenContainedIn:[UISearchBar class], nil] setBorderStyle:UITextBorderStyleNone];
   self.searchBar.layer.borderColor=[UIColor clearColor].CGColor;

OTHER TIPS

I hope that helps you.

self.searchBar.searchBarStyle = UISearchBarStyleMinimal;

or

[searchBar setBackgroundImage:[UIImage new]];
[searchBar setTranslucent:YES];

check it,,

    self.navigationController.navigationBar.translucent = NO; // If you have a navBar
    self.searchDisplayController.searchBar.translucent = NO;
[self.mySearchBar setBackgroundImage:[[UIImage alloc] init]];

Hope this will work for you.

I've packaged a very simple and nifty extension for Swift 3 that extend the UISearchBar to be translucent. It involves the use of two very simple extensions.

You can download it from this github repository.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top