Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot change search bar background color

I have a search bar:

let searchBar:UISearchBar = UISearchBar(frame: CGRectMake((searchView.frame.width - UIScreen.mainScreen().bounds.width / 1.6) / 2, 0, UIScreen.mainScreen().bounds.width / 1.6, 24)) 

and I want to change text input part background color. For this I've tried:

searchBar.barTintColor = UIColor(red: 0/255, green: 74/255, blue: 103/255, alpha: 1)  searchBar.backgroundColor = UIColor.redColor() 

but these both variants do not work. How can I change background color of my UISearchBar textInput part and what I did wrong?

like image 649
Orkhan Alizade Avatar asked Nov 17 '15 07:11

Orkhan Alizade


People also ask

How do I change the search bar color in IOS Swift?

Change Search Bar Default Image Color The left hand default search image in UISearchBar represents the left view of the UITextField. The Image is rendered to change it to the desired colour. @IBOutlet weak var searchBar: UISearchBar! Hope it will help you in customising the UISearchBar in your app.

How do I change the background color of a textfield in Swift?

To change the background color in a UITextField you first need to use a different style of text field to the "rounded" style (such as the "no border" style) either in Interface Builder or programmatically.


1 Answers

Like @aliamcami, all the answers before did not work as I would expect, either the answer did not work for me or it works but it needs too much "dummy" code. So I share another answer wrote in Swift 4 with simplified logic:

for textField in searchController.searchBar.subviews.first!.subviews where textField is UITextField {     textField.subviews.first?.backgroundColor = .white     textField.subviews.first?.layer.cornerRadius = 10.5 //I set 10.5 because is approximately the system value     textField.subviews.first?.layer.masksToBounds = true     //Continue changing more properties... } 

textField.subviews.first is the "_UISearchBarSearchFieldBackgroundView" subview which add visual effects behind the UIFieldEditor.


Edited

After some development and many bugs, I finished with this elegant solution (that I am sure Apple would not be happy to approve, but I do not know) that works from iOS 10 to iOS 12:

if let textField = searchBar.value(forKey: "searchField") as? UITextField {     textField.backgroundColor = myColor     //textField.font = myFont     //textField.textColor = myTextColor     //textField.tintColor = myTintColor     // And so on...          let backgroundView = textField.subviews.first     if #available(iOS 11.0, *) { // If `searchController` is in `navigationItem`         backgroundView?.backgroundColor = UIColor.white.withAlphaComponent(0.3) //Or any transparent color that matches with the `navigationBar color`         backgroundView?.subviews.forEach({ $0.removeFromSuperview() }) // Fixes an UI bug when searchBar appears or hides when scrolling     }     backgroundView?.layer.cornerRadius = 10.5     backgroundView?.layer.masksToBounds = true     //Continue changing more properties... } 

When the searchBar is in the tableHeaderView the above code can be called in viewWillAppear, but if it is in the navigationItem on iOS 11 and above it should be called in viewDidAppear.

like image 109
Ángel Téllez Avatar answered Oct 03 '22 20:10

Ángel Téllez