如何将UiSearchBar的背景颜色更改为黑色-SWIFT

我以编程方式创build了UISearchBar,如下所示,并且button显示为第二个图像。如何清除search栏的白色并将背景色设置为黑色

var categorySearchBar : UISearchBar = UISearchBar() categorySearchBar.frame = CGRectMake(0, 20, self.view.frame.size.width, 30) categorySearchBar.layer.borderColor = UIColor.grayColor().CGColor categorySearchBar.layer.borderWidth = 1.0 categorySearchBar.placeholder = "SEARCH" categorySearchBar.barTintColor = UIColor.blackColor() self.view.addSubview(categorySearchBar) 

图1

图2

您可以使用UISearchBar的 barStyle属性,如果您使用的是故事板,则可以使用Attribute Inspector更改条形样式,或使用以下代码段。 这可能会帮助队友:)

 categorySearchBar.barStyle = UIBarStyle.Black