Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Border of UISearchBar in iOS7

I'm trying to remove border of UISearchBar in iOS 7. In iOS 6 it's working fine. I created the UISearchBar programatically. I tried almost every thing from Stack Overflow and Google.

SearchBar looking right now
SearchBar looking right now

What i want to achieve
What i want to achieve

I tried all these stuffs mentioned below

searchBar.layer.borderWidth = 1; searchBar.layer.borderColor = [[UIColor whiteColor] CGColor]; 

and

for (id img in searchBar.subviews) {             if ([img isKindOfClass:NSClassFromString(@"UISearchBarBackground")])      {                  [img removeFromSuperview];          } }  

and

for (UIView *sub in self.tableView.tableHeaderView.subviews) {     if ([sub isKindOfClass:[UIImageView class]]) {         sub.hidden = YES;     } }   

but still no success.

like image 596
iEngineer Avatar asked Nov 11 '13 05:11

iEngineer


People also ask

How do I use UISearchBar?

You can add a UISearchBar as you would with any other control by dragging one to your view controller in interface builder or by programmatically adding it. The delegate property of search bar must be set to an object that implements UISearchBarDelegate.


2 Answers

Set Search Style = minimal in Search Bar properties in IB

Or

Swift: searchBar.searchBarStyle = UISearchBarStyleMinimal;  Swift 3: searchBar.searchBarStyle = .minimal; 
like image 150
nerowolfe Avatar answered Sep 20 '22 19:09

nerowolfe


Setting searchBarStyle to UISearchBarStyleMinimal messed up my color setup, so doing this instead fixed the issue.

[self.searchField setBackgroundImage:[[UIImage alloc]init]]; 

For those looking for this option in Swift 4:

searchField.setBackgroundImage(UIImage(), for: .any, barMetrics: UIBarMetrics.default) 
like image 27
Rich Fox Avatar answered Sep 18 '22 19:09

Rich Fox