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

问题描述 投票:0回答:2

我以编程方式创建了 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)

figure 1

figure 2

ios swift uisearchbar
2个回答
8
投票

您可以使用 UISearchBarbarStyle 属性,如果您使用故事板,您可以使用属性检查器更改栏样式,或使用以下代码段。这可能对交配有帮助:)

categorySearchBar.barStyle = UIBarStyle.Black

0
投票

以防万一有人仍在努力将搜索栏保持为黑暗模式,只需添加此

self.navigationItem.searchController?.searchBar.overrideUserInterfaceStyle
= .dark
© www.soinside.com 2019 - 2024. All rights reserved.