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
Swift 2.1 in Xcode 7.2, this worked for me.
self.searchController.searchBar.backgroundImage = UIImage()
My full code below.
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.sizeToFit()
tableView.sectionIndexBackgroundColor = UIColor(red: 0/255, green: 181/255, blue: 229/255, alpha: 1.0)
self.searchController.searchBar.backgroundColor = UIColor(red: 0/255, green: 181/255, blue: 229/255, alpha: 1.0)
self.searchController.searchBar.barTintColor = UIColor(red: 0/255, green: 181/255, blue: 229/255, alpha: 1.0)
self.searchController.searchBar.backgroundImage = UIImage()
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
Neither only barTintColor
, backgroundImage
nor backgroundColor
alone were doing it for me, but doing them all together worked for me:
self.searchBar.translucent = NO;
self.searchBar.barTintColor = [styleManager currentSearchBarTintColor];
self.searchBar.backgroundImage = [UIImage new];
self.searchBar.backgroundColor = [styleManager currentSearchBarTintColor];
Set Search Style = minimal in Search Bar properties in IB
Or
Swift:
searchBar.searchBarStyle = UISearchBarStyleMinimal;
Swift 3:
searchBar.searchBarStyle = .minimal;
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)
Okay. There are so many answers, but they are too complex. I've found this solution:
Swift 3 (4)
searchBar.setBackgroundImage(UIImage(), for: .top, barMetrics: .default)
searchBar.backgroundColor = .primary
Where .primary is
extension UIColor {
static var primary:UIColor {
return "#5163F4".color
}
}
self.searchBar.translucent = NO;
self.searchBar.opaque = NO;
if ([self.searchBar respondsToSelector:@selector(setSearchBarStyle:)]) {
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
}
// iOS 7 remove 1 px bottom border
if ([self.searchBar respondsToSelector:@selector(setBarTintColor:)]) {
self.searchBar.barTintColor = [UIColor clearColor];
}
self.searchBar.barStyle = UIBarStyleDefault;
// to remove the 1px bottom border iOS 5, 6
[self.searchBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] andSize:CGSizeMake(1.0f, 1.0f)]];
The order of code seems does matter. It doesn't work if I set the barStyle before the searchBarStyle.