我在视图控制器中得到了这些代码行。
该代码适用于UISearchBar文本颜色,它适用于
let textFieldInsideSearchBar = searchBar.value(forKey: “searchField”) as? UITextField
textFieldInsideSearchBar?.textColor = UIColor.white
该代码适用于UISearchBar占位符文本颜色,它不起作用
let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: “placeholderLabel”) as? UILabel
textFieldInsideSearchBarLabel?.textColor = UIColor.white
这是占位符文本颜色等的工作代码。
该代码适用于UISearchBar放大镜图标的颜色,它适用于
let glassIconView = textFieldInsideSearchBar?.leftView as? UIImageView
glassIconView?.image = glassIconView?.image?.withRenderingMode(.alwaysTemplate)
glassIconView?.tintColor = UIColor.white
该代码是UISearchBar清除按钮(x(颜色的代码,它不起作用
let clearButton = textFieldInsideSearchBar?.value(forKey: “clearButton”) as! UIButton
clearButton.setImage(clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate), for: .normal)
clearButton.tintColor = UIColor.white
我发现那个版本的清除按钮颜色变化在Xcode 11上不起作用。设法拥有工作版本。您必须为取消按钮导入资产。之后,您需要将图标的"渲染为"属性更改为"资源"文件夹中的"模板图像"。
let clearButton = textFieldInsideUISearchBar?.value(forKey: "clearButton") as! UIButton
clearButton.setImage(UIImage(named: "yourIconName"), for: .normal)
clearButton.tintColor = .white
此外,我已经尝试了几乎所有我能找到的代码。不知怎的,它们适用于文本和放大镜,但不适用于其他两种。背景暗的时候它们看起来很难看。有没有更好的方法来更改清除按钮和占位符文本的颜色?
Xcode版本11.1Swift 5.1
对于占位符颜色,请参阅此。
要更改搜索栏清除图像色调的颜色,需要使用新图像设置搜索栏。
yourSearchBar.setImage(normalImage, for: .clear, state: .normal)
yourSearchBar.setImage(highlightedImage, for: .clear, state: .highlighted)
UISearchBarController
为iOS 13定制尝试this