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
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.
Solution 1:
Set Search Style = minimal in Search Bar properties in IB
Or
Swift:
searchBar.searchBarStyle = UISearchBarStyleMinimal;
Swift 3:
searchBar.searchBarStyle = .minimal;
Solution 2:
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)
Solution 3:
For Swift, these 2 lines are enough:
self.search.isTranslucent = false
self.search.backgroundImage = UIImage()
And then, apply required color that you want:
self.search.barTintColor = .red
Solution 4:
I found the solution: set the barTintColor
of UISearchBar
to clearColor
topSearchBar.barTintColor = [UIColor clearColor];