Reputation: 143
I am currently working on an app that worked fine until ios7 came along. The search bar used to be transparent and blended into the blue background of the navigation bar. Now that I am working in ios7, the nav bar is blue, however the search bar has a gray background to it. How do I make it blue or transparent?
Here is an image:
Upvotes: 14
Views: 22587
Reputation: 171
Swift 4.2
You can use this extension to change Font and Background color of the SearchBar.
extension UISearchBar {
var textField: UITextField? {
let subViews = subviews.flatMap { $0.subviews }
guard let tf = (subViews.filter { $0 is UITextField }).first as? UITextField else { return nil }
return tf
}
func setTextColor(color: UIColor) {
textField?.textColor = color
}
func setBackgroundColor(color: UIColor) {
textField?.backgroundColor = color
}
}
Upvotes: 0
Reputation: 10503
You can set "Bar Tint" to "Clear Color" in Interface Builder (.xib):
It can also be done in code:
self.searchBar.barTintColor = [UIColor clearColor];
Upvotes: 12
Reputation: 1764
To make it a flat color, you simply need to remove the UISearchBarBackground view.
I created a recursive method to properly clean the search bar.
- (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view
{
for (UIView *subview in [view subviews]) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break; //To avoid an extra loop as there is only one UISearchBarBackground
} else {
[self removeUISearchBarBackgroundInViewHierarchy:subview];
}
}
}
You can simply send your search bar to the method and change the color afterward.
[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.backgroundColor = yourUIColor;
Upvotes: 5
Reputation: 4744
Try this:
if(IOS_7)
{
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
}
Upvotes: 36
Reputation: 1297
**Edit - This worked for me in iOS 7
// Set the color to whatever blue color that is in your screenshot
self.searchBar.backgroundImage = [UIImage imageWithColor:[UIColor redColor] cornerRadius:5.0f];
If you want all of your search bar's to be a certain color do this:
// Put this in your app delegate's didFinishLaunchingWithOptions method
// Whatever color you want for searchBarColor
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { // For iOS 7
UIColor *searchBarColor = [UIColor blueColor];
[[UISearchBar appearance] setBackgroundColor:searchBarColor];
}
If you just want that particular search bar background to be a color:
// Set it in your viewDidLoad method of your controller
// Replace the yourSearchBar property with whatever you're doing to instantiate the search bar
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { // For iOS 7
{
UIColor *searchBarColor = [UIColor blueColor];
self.yourSearchBar.backgroundColor = searchBarColor;
}
Upvotes: -1