我以编程方式创建了 UISearchBar,如下所示,该按钮似乎是第二张图片。如何清除搜索栏的白色并将背景颜色设置为黑色
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)
您可以使用 UISearchBar 的 barStyle 属性,如果您使用故事板,您可以使用属性检查器更改栏样式,或使用以下代码段。这可能对交配有帮助:)
categorySearchBar.barStyle = UIBarStyle.Black
以防万一有人仍在努力将搜索栏保持为黑暗模式,只需添加此
self.navigationItem.searchController?.searchBar.overrideUserInterfaceStyle
= .dark