14

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:

enter image description here

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
Freddy
  • 143
  • 1
  • 1
  • 6

5 Answers5

36

Try this:

if(IOS_7)
{
    self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
}
Quang Hà
  • 4,613
  • 3
  • 24
  • 40
  • Thanks this did the trick. Much appreciated. Thank you too all who contributed to this question as well. – Freddy Oct 08 '13 at 03:50
  • Quick Questions: is there anyway to change the color of the text of the search bar? – Freddy Oct 08 '13 at 04:09
  • Check this answer: http://stackoverflow.com/a/18973045/1615838 , you only need to change the text color of the text field in search bar. – Quang Hà Oct 08 '13 at 06:38
  • What's the identifier for the "IOS_7"? I'm getting error for `use of undeclared identifier`. – Chisx Jan 11 '14 at 07:42
  • It's macro to prevent force closed in iOS < 7.0. You can check this link http://stackoverflow.com/questions/7848766/how-can-we-programmatically-detect-which-ios-version-is-device-running-on – Quang Hà Jan 11 '14 at 07:59
  • @GabrielCartier you means 7.1? – Quang Hà Mar 26 '14 at 15:28
  • Nope, even 7.0, look at my answer, the structure of the searchBar is different since iOS7. – Gabriel Cartier Mar 28 '14 at 18:16
12

You can set "Bar Tint" to "Clear Color" in Interface Builder (.xib):

enter image description here

It can also be done in code:

self.searchBar.barTintColor = [UIColor clearColor];
devios1
  • 36,899
  • 45
  • 162
  • 260
David Douglas
  • 10,377
  • 2
  • 55
  • 53
  • 5
    I think this is the right answer (setting the barTintColor) self.searchBar.barTintColor = [UIColor clearColor]; – user1105951 Oct 16 '13 at 12:50
5

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;
Gabriel Cartier
  • 1,734
  • 20
  • 22
0

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
    }
}
Get Schwifty
  • 161
  • 2
  • 3
-1

**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;
}
Matt Tang
  • 1,297
  • 11
  • 17
  • Try this then: self.searchBar.backgroundImage = [UIImage imageWithColor:[UIColor redColor] cornerRadius:0.0f]; – Matt Tang Oct 03 '13 at 22:25
  • 3
    +[UIImage imageWithColor:cornerRadius] isn't in the native APIs, its an addition from [FlatUIKit](https://github.com/Grouper/FlatUIKit) – neilkimmett Oct 14 '13 at 15:17