寻找更新(经过iOS的13)的回答,我没有找到这个简单问题的任何解决方案:
如何更改textColor占位符的一个UISearchBar?
我的应用程序不处理Light/Dark模式。我不希望系统更改我的UIPlaceHolder文本颜色。我希望它总是white。
if #available(iOS 13.0, *) {
let attrString = NSMutableAttributedString(string: "My PlaceHolder")
attrString.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], range: NSRange(location: 0, length: attrString.length))
searchController.searchBar.searchTextField.attributedPlaceholder = attrString
}
Run Code Online (Sandbox Code Playgroud)
我希望这段代码可以工作。我认为新属性searchTextField会使自定义我的UISearchBar.
编辑:
这种代码在viewDidAppear方法中有效:
if #available(iOS 13.0, *) {
searchController.searchBar.searchTextField.attributedPlaceholder = NSAttributedString(string: "My PlaceHolder", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white.withAlphaComponent(0.80)])
}
Run Code Online (Sandbox Code Playgroud)
问题是当您上下滚动时颜色会发生变化。
我想randomly选择2种颜色。但它必须是随机cold颜色和随机warm颜色。
我正在使用它extension来获取随机颜色:
extension UIColor {
static var random: UIColor {
return UIColor(red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1),
alpha: 1.0)
}
}
Run Code Online (Sandbox Code Playgroud)
根据这篇文章,我们可以假设:
if (B > R) {
color = cool
} else {
color = warm
}
Run Code Online (Sandbox Code Playgroud)
如何修改我的随机扩展以能够选择随机暖色或随机冷色。
有什么建议吗?