Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize UISearchBar: Trying to get rid of the 1px black line underneath the search bar

My question is already specified in the title: I would like to get rid of the black line drawn on the bottom of the UISearchBar. Any ideas?

Here's an image of what I mean:

search bar with black bottom line

UPDATE:

I think that the line is part of the UITableView's tableHeaderView. I still don't know how to remove it.

like image 408
strave Avatar asked Oct 01 '11 13:10

strave


4 Answers

Try this

 searchBar.layer.borderWidth = 1;

 searchBar.layer.borderColor = [[UIColor lightGrayColor] CGColor];
like image 71
Ayush Goel Avatar answered Nov 02 '22 06:11

Ayush Goel


Why:

So, I've dug into the API's trying to figure out why this is happening. Apparently whomever wrote the UISearchBar API is rasterizing the lines onto an image and setting it as it's backgroundImage.

Solution:

I propose a simpler solution, if you want to set the backgroundColor and get rid of the hairlines:

searchBar.backgroundColor = <#... some color #>
searchBar.backgroundImage = [UIImage new];

Or if you just need a background image without the hairlines:

searchBar.backgroundImage = <#... some image #>
like image 52
Oxcug Avatar answered Nov 02 '22 07:11

Oxcug


I have 0.5px black horizontal lines both on top and on the bottom of my UISearchBar. The only way I had so far to get rid of them is by setting its style to Minimal:

mySearchBar.searchBarStyle = UISearchBarStyleMinimal;
like image 42
ancajic Avatar answered Nov 02 '22 07:11

ancajic


Solution for

XCode 10.1 Swift 4.2

enter image description here

like image 30
swift2geek Avatar answered Nov 02 '22 07:11

swift2geek