iosuisearchbarbartintcolor

How to get default gray color of UISearchBar


My layout places the UISearchController's search bar as a subview of my table view header, alongside other custom controls (buttons, etc.).

In order to make all the controls inside the header view to "blend in", I want to set the header view's background color to match that of the search bar (that is, the outer rim's light gray, not the input textfield's white).

I've tried querying both the search bar's backgroundColor and barTintColor propErties in viewDidLoad(), but both return nil, and my header view ends up white.

I could hard code the color (ColorPicker reports it to be RGB (199, 199, 204), at least on the Simulator), but that's not an elegant solution and will break one day when Apple changes the appearance.


Solution

  • The background for the search bar is actually an image so you cannot retrieve it's background color. However, if you just want it to blend in then you can change the imageView to be transparent and set the background color to anything you wish. The UIImageView was found to be here using the view debugger in Xcode.

    In Swift 2.0

    (searchBar.subviews[0].subviews[0] as! UIImageView).alpha = 0