无法更改search栏的背景颜色

我有一个search栏:

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

我想改变文本input部分的背景颜色。 为此,我试过了:

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

但是这两个变体都不起作用。 我怎么能改变我的UISearchBar textInput部分的背景颜色,我做错了什么?

请参阅演示项目: 演示

您将在ViewDidLoad中添加以下代码并更改文本字段背景颜色RED,

 for subView in searchBar.subviews { for subView1 in subView.subviews { if subView1.isKindOfClass(UITextField) { subView1.backgroundColor = UIColor.redColor() } } } 

SearchBar中的TextField的红色。

在这里输入图像说明

对于Swift 3+,使用这个:

 for subView in searchController.searchBar.subviews { for subViewOne in subView.subviews { if let textField = subViewOne as? UITextField { subViewOne.backgroundColor = UIColor.red //use the code below if you want to change the color of placeholder let textFieldInsideUISearchBarLabel = textField.value(forKey: "placeholderLabel") as? UILabel textFieldInsideUISearchBarLabel?.textColor = UIColor.blue } } } 

如果你只想在你的ViewController中改变它,而不想在其他地方效果,那么使用

 for view in searchBar.subviews { for subview in view.subviews { if subview .isKindOfClass(UITextField) { let textField: UITextField = subview as! UITextField textField.backgroundColor = UIColor.redColor() } } } 

但是,如果你想要它是整个应用程序的变化,并针对iOS 9.0或更高版本,那么应该使用appearanceWhenContainedInInstancesOfClasses像

 UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).backgroundColor = UIColor.redColor() 

像这样

 let searchBar:UISearchBar = UISearchBar(frame: CGRectMake((searchView.frame.width - UIScreen.mainScreen().bounds.width / 1.6) / 2, 0, UIScreen.mainScreen().bounds.width / 1.6, 24)) let searchTextField = searchBar.valueForKey("_searchField") as? UITextField searchTextField?.backgroundColor = UIColor.redColor() 

这个问题在networking上的一个非常普遍的答案是上面描述的这个解决scheme:

 for subView in searchBar.subviews { for subViewInSubView in subView.subviews { if subViewInSubView.isKindOfClass(UITextField) { subViewInSubView.backgroundColor = UIColor.purpleColor() } } } 

但是对于我使用XCode 7和iOS 9并不适用,而且我注意到在networking上有许多人报告说它们也不适合他们。 我发现,UITextField子视图没有创build(或至less它不是作为子视图添加),直到它被引用,它可以引用的一种方式是占位符字段,例如,可以添加此行之前searchUITextField类的子视图:

 searchBar.placeholder = searchBar.placeholder 

仅使用Apple API执行此操作的方法是创build一个图像并使用setSearchFieldBackgroundImage

 self.searchBar.setSearchFieldBackgroundImage(UIImage(named: "SearchFieldBackground"), for: UIControlState.normal) 

如果您创build一个圆angular矩形并dynamic显示和隐藏button,它甚至可以正确地设置angular。

使用此图片的示例: 在这里输入图像说明

在这里输入图像说明

设置你想要的颜色SWIFT 3

 public extension UISearchBar { public func setStyleColor(_ color: UIColor) { tintColor = color guard let tf = (value(forKey: "searchField") as? UITextField) else { return } tf.textColor = color if let glassIconView = tf.leftView as? UIImageView, let img = glassIconView.image { let newImg = img.blendedByColor(color) glassIconView.image = newImg } if let clearButton = tf.value(forKey: "clearButton") as? UIButton { clearButton.setImage(clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate), for: .normal) clearButton.tintColor = color } } } extension UIImage { public func blendedByColor(_ color: UIColor) -> UIImage { let scale = UIScreen.main.scale if scale > 1 { UIGraphicsBeginImageContextWithOptions(size, false, scale) } else { UIGraphicsBeginImageContext(size) } color.setFill() let bounds = CGRect(x: 0, y: 0, width: size.width, height: size.height) UIRectFill(bounds) draw(in: bounds, blendMode: .destinationIn, alpha: 1) let blendedImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return blendedImage! } } 

FWIW我正在通过创build一个名为textField的计算variables来解决这个问题。

 extension UISearchBar { var textField: UITextField? { return subviews.first?.subviews.first(where: { $0.isKind(of: UITextField.self) }) as? UITextField } } 

只要在操场上尝试下面的代码。 如果仍然不行,请在您的问题中添加更多代码。

 let searchView = UIView(frame: CGRect(x: 0.0,y: 0.0, width: 100, height: 50)) let searchBar:UISearchBar = UISearchBar(frame: CGRectMake((searchView.frame.width - UIScreen.mainScreen().bounds.width / 1.6) / 2, 0, UIScreen.mainScreen().bounds.width / 1.6, 24)) for subView in searchBar.subviews { for subViewInSubView in subView.subviews { if subViewInSubView.isKindOfClass(UITextField) { subViewInSubView.backgroundColor = UIColor.purpleColor() } } } 

我这样做(Swift 3+解决scheme):

 let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as? UITextField textFieldInsideSearchBar?.backgroundColor = UIColor.red