Remove Border of UISearchBar in iOS7
IosUisearchbarIos Problem Overview
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.
Ios Solutions
Solution 1 - Ios
Set Search Style = minimal in Search Bar properties in IB
Or
Swift:
searchBar.searchBarStyle = UISearchBarStyleMinimal;
Swift 3:
searchBar.searchBarStyle = .minimal;
Solution 2 - Ios
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 - Ios
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 - Ios
I found the solution: set the barTintColor
of UISearchBar
to clearColor
topSearchBar.barTintColor = [UIColor clearColor];
Solution 5 - Ios
>This only works if .borderStyle = UITextBorderStyleLine
;
.
My experiments conclusion,
-
If you're on iOS7 and above, and if you'll set,
searchBar.barTintColor = [UIColor clearColor];
then you'll not able to customise background colour ofUISearchBar
. -
if you'll set
searchBarStyle
toUISearchBarStyleMinimal
then it'll mess the color ofUISearchBar
as said by @Rich Fox.
So, [self.searchField setBackgroundImage:[[UIImage alloc]init]];
solution to remove border.
Update with sample:
UISearchBar *search = [[UISearchBar alloc] init];
search.tag = kTagSearchBar;
search.delegate = self;
search.tintColor = [UIColor redColor];
search.searchBarStyle = UISearchBarStyleMinimal;
search.frame = CGRectMake(0, 0, 320, 50);
search.placeholder = @"Search";
search.barTintColor = [UIColor blueColor];
search.translucent = NO;
search.opaque = NO;
search.showsCancelButton = NO;
[search setBackgroundImage:[[UIImage alloc] init]];
[self.view addSubview:search];
//customize textfield inside UISearchBar
@try {
for (id object in [[[search subviews] firstObject] subviews])
{
if (object && [object isKindOfClass:[UITextField class]])
{
UITextField *textFieldObject = (UITextField *)object;
textFieldObject.backgroundColor = [UIColor whiteColor];
textFieldObject.borderStyle = UITextBorderStyleLine;
textFieldObject.layer.borderColor = [UIColor blueColor].CGColor;
textFieldObject.layer.borderWidth = 1.0;
break;
}
}
}
@catch (NSException *exception) {
NSLog(@"Error while customizing UISearchBar");
}
@finally {
}
will give you:
Solution 6 - Ios
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];
Solution 7 - Ios
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
}
}
Solution 8 - Ios
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.
Solution 9 - Ios
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
Solution 10 - Ios
- (void)createNavigationBar
{
_searchBar = [[UISearchBar alloc]init];
_searchBar.backgroundColor = [UIColor whiteColor];
_searchBar.placeholder = @"Search";
_searchBar.translatesAutoresizingMaskIntoConstraints = NO;
self.navigationItem.titleView = _searchBar;
NSDictionary *viewsDictionary = @{@"SearchBar":_searchBar};
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[SearchBar(30)]|"
options:0
metrics:nil
views:viewsDictionary];
NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[SearchBar]-15-|"
options:0
metrics:nil
views:viewsDictionary];
[_searchBar.superview addConstraints:constraint_POS_V];
[_searchBar.superview addConstraints:constraint_POS_H];
}