如何获取UISearchBar的颜色



正如我们所知,我们通过这行代码更改SearchBar的文本颜色,效果很好:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.blue]

颜色的占位符是UIColor.blue

问题是:

如何获取特定颜色

我已尝试将UIColor.blue更改为UIColor.init(red: 200, green: 200, blue: 200, alpha: 1)

然而,无论我向init()提供多少个红/绿/蓝/阿尔法,我都无法获取颜色。

是否存在类似.blue/.red/.ellow的枚举限制?

我做错了什么?

public extension UISearchBar 
{
public func SetSearchbarcolor(srchcolor: UIColor)
{
let Search = subviews.flatMap { $0.subviews }
guard let Searchfilter = (Search.filter { $0 is UITextField }).first as? UITextField else { return }
Searchfilter.textColor = srchcolor
}
}

Output : Searchbar_name.SetSearchbarcolor(srchcolor: UIColor.colorname)

请尝试而不是UIColor.init

UIColor(red: 100/255, green: 100/255, blue: 100/255, alpha: 1)

最新更新