Reputation: 7549
How can I make the search bar tint color full blue?
I've tried:
searchBar.opaque = true
also
searchBar.translucent = false
But it does not work. Why?
Upvotes: 3
Views: 1486
Reputation: 73186
From the documentation on .opaque
var opaque: Bool { get set }
A Boolean value that determines whether the view is opaque.
...
You only need to set a value for the opaque property for subclasses of UIView that draw their own content using the drawRect: method. The opaque property has no effect for system provided classes such as UIButton, UILabel, UITableViewCell, etc.
Hence, the .opaque properties will have no effect on a native UISearchBar; therefor, in you're example above, searchBar.opaque
has no effect.
Regarding the .translucent
property, the documentation states:
var translucent: Bool { get set }
A Boolean value that indicates whether the search bar is translucent (true) or not (false).
The default value is true. If the search bar has a custom background image, the default is true if any pixel of the image has an alpha value of less than 1.0, and false otherwise.
If you set this property to true on a search bar with an opaque custom background image, the search bar will apply a system opacity less than 1.0 to the image.
If you set this property to false on a search bar with a translucent custom background image, the search bar provides an opaque background for the image using black if the search bar has UIBarStyleBlack style, white if the search bar has UIBarStyleDefault, or the search bar’s barTintColor if a custom value is defined.
Hence, to achieve a transparent background for your search bar, you need to set also a background image for it, which has been described previously in the following SO thread
Using Mike:s answer in the linked thread (Obj-C), we can adapt to swift according to:
searchBar.barTintColor = UIColor.clearColor()
searchBar.backgroundImage = UIImage()
searchBar.translucent = false
This should achieve a transparent search bar.
Upvotes: 6