29

Upgraded from Xcode 5 to 6 and now my search bar tint is black.

Tried to change it through storyboard right pane > "Bar Tint" to clear color, but it's still black.

Also tried programmatically:

[self.searchBar setTintColor:[UIColor clearColor]];

Still black :(

Any ideas?

Pang
  • 9,564
  • 146
  • 81
  • 122
ox_
  • 805
  • 1
  • 7
  • 14
  • 1
    I think you want to set the [barTintColor](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/index.html#//apple_ref/occ/instp/UISearchBar/barTintColor). – CrimsonChris Sep 26 '14 at 17:18
  • Do you want the search bar's *background* to be clear, or the search bar's *textfield's* background to be clear? – Mike Sep 28 '14 at 15:30
  • The search bar background - the orange rectangle around - see below – ox_ Sep 29 '14 at 09:29
  • Did you see the second part of my answer below, where I make it clear? – Mike Sep 29 '14 at 13:29

5 Answers5

69

The tintColor property on search bars, much like UINavigationBar, changes the color of the buttons, as well as changes the color of the blinking cursor, not the actual search bar background. What you want to use is the barTintColor property.

searchbar.barTintColor = [UIColor orangeColor];
searchbar.tintColor = [UIColor greenColor];

Produces the following ugly, yet informative, result:

enter image description here

If you want to have a completely transparent search bar, you need to set the background image as well:

searchbar.barTintColor = [UIColor clearColor];
searchbar.backgroundImage = [UIImage new];

enter image description here

EDIT: I would strongly advise against traversing and modifying the subviews of any UIKit object, as has been proposed in other answers. From Apple's documentation:

For complex views declared in UIKit and other system frameworks, any subviews of the view are generally considered private and subject to change at any time. Therefore, you should not attempt to retrieve or modify subviews for these types of system-supplied views. If you do, your code may break during a future system update.

https://developer.apple.com/documentation/uikit/uiview/1622614-subviews

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mike
  • 9,765
  • 5
  • 34
  • 59
  • 1
    searchbar.barTintColor = [UIColor orangeColor]; made it orange, however when using [UIColor clearColor] it is still black from some strange reason – ox_ Sep 26 '14 at 17:40
  • See my edit, you need to change the background view. – Mike Sep 26 '14 at 18:06
  • 2
    For those who are still having issues with this, I actually had to set searchBar.scopeBarBackgroundImage = [UIImage new]; as well. – csundman Oct 24 '14 at 21:50
  • 1
    Nicely done - saved me from having to subclass, and I agree that all things being equal it's a bad idea to be removing views as it's impossible to know what the future consequences would be. – BonanzaDriver Apr 16 '15 at 13:25
  • for me it didn't work. so I put it a uiview under the search bar which has the color I wanted then I make the search bar transparent ( both bar tint and background color ). – Niib Fouda Oct 16 '17 at 13:48
7

I got to change it on iOS 9 using Swift 2.0 this way:

    let image = UIImage()

    searchBar.setBackgroundImage(image, forBarPosition: .Any, barMetrics: .Default)
    searchBar.scopeBarBackgroundImage = image
darqueos
  • 121
  • 2
  • 6
  • Good solution. Important to note, that this solution will only work if the searBar is Translucent. – Kashif Jan 01 '17 at 19:23
2

On Swift 3:

Setting up the background of the searchBar to an empty image:

    searchBar.setBackgroundImage(image, for: .any, barMetrics: .default)
    searchBar.scopeBarBackgroundImage = image
Gilad Brunfman
  • 3,452
  • 1
  • 29
  • 29
0

for programmatically change search bar tint color :

    if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
        textfield.textColor = #colorLiteral(red: 0.3921568627, green: 0.3921568627, blue: 0.3921568627, alpha: 1)
        textfield.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
    }

for storyboard :

enter image description here

krishnan muthiah pillai
  • 2,711
  • 2
  • 29
  • 35
-1

I've been seeing this problem as well on iOS8 / XCode 6. I did not get the search bar translucent. searchbar.barTintColor or searchbar.tintColor setting to clear color did not help or shows a black searchbar.

The only workaround i found was to set a translucent background png image (alpha=0.0) and setting searchbar.barTintColor to clear color.