Can't change search bar tint color to be transparent in iOS 8

21,417

Solution 1

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

Solution 2

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

Solution 3

On Swift 3:

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

    searchBar.setBackgroundImage(image, for: .any, barMetrics: .default)
    searchBar.scopeBarBackgroundImage = image
Share:
21,417
ox_
Author by

ox_

Updated on February 28, 2020

Comments

  • ox_
    ox_ over 4 years

    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?