如何更改a的文本颜色UISearchBar
?
Answers:
您必须访问UITextField
UISearchBar内部。您可以通过使用valueForKey("searchField")
var textFieldInsideSearchBar = yourSearchbar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar?.textColor = yourcolor
Swift 3更新
let textFieldInsideSearchBar = yourSearchbar.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.textColor = yourcolor
searchField
通过KVC访问。
在Swift 4中为我工作:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
Swift 4.2,IOS 12:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
public extension UISearchBar {
public func setTextColor(color: UIColor) {
let svs = subviews.flatMap { $0.subviews }
guard let tf = (svs.filter { $0 is UITextField }).first as? UITextField else { return }
tf.textColor = color
}
}
这适用于我在iOS 11,Xcode 9上:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).textColor = UIColor.blue
我在中写了它,AppDelegate
但是如果您也将它放在其他地方,我想它也会起作用。
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTextColor:[UIColor blueColor]];
这当然是Objective-C,显然使用Swift进行了翻译appearance(whenContainedInInstancesOf:)
我认为最方便的方法是在中设置textColor
属性UISearchBar
。
斯威夫特3.0
extension UISearchBar {
var textColor:UIColor? {
get {
if let textField = self.value(forKey: "searchField") as?
UITextField {
return textField.textColor
} else {
return nil
}
}
set (newValue) {
if let textField = self.value(forKey: "searchField") as?
UITextField {
textField.textColor = newValue
}
}
}
}
用法
searchBar.textColor = UIColor.blue // Your color
迅捷2.3
extension UISearchBar {
var textColor:UIColor? {
get {
if let textField = self.valueForKey("searchField") as? UITextField {
return textField.textColor
} else {
return nil
}
}
set (newValue) {
if let textField = self.valueForKey("searchField") as? UITextField {
textField.textColor = newValue
}
}
}
}
您可以将其用作:
searchBar.textColor = UIColor.blueColor() // Your color
从Xcode 11和iOS 13开始,可以直接访问文本字段:
searchBar.searchTextField
您可以编写一些代码以使其始终可访问,如下所示。
extension UISearchBar {
public var textField: UITextField? {
if #available(iOS 13.0, *) {
return searchTextField
}
guard let firstSubview = subviews.first else {
assertionFailure("Could not find text field")
return nil
}
for view in firstSubview.subviews {
if let textView = view as? UITextField {
return textView
}
}
assertionFailure("Could not find text field")
return nil
}
}
您也可以将其设置为带有致命错误的非可选选项,此代码自iOS 7到iOS 13GM均经过测试。但是我只会选择可选版本。
extension UISearchBar {
public var textField: UITextField {
if #available(iOS 13.0, *) {
return searchTextField
}
guard let firstSubview = subviews.first else {
fatalError("Could not find text field")
}
for view in firstSubview.subviews {
if let textView = view as? UITextField {
return textView
}
}
fatalError("Could not find text field")
}
}
您可以通过访问searchBar内的UITextField来执行此操作,然后可以更改其背景颜色,文本颜色和所有其他UITextField属性
添加以下扩展名以访问textField
extension UISearchBar {
/// Return text field inside a search bar
var textField: UITextField? {
let subViews = subviews.flatMap { $0.subviews }
guard let textField = (subViews.filter { $0 is UITextField }).first as? UITextField else { return nil
}
return textField
}
}
我尝试了上面写的一些解决方案,但是它们没有用(我想还是可以了)。
如果您只想处理iOS 13:
mySearchBar.searchTextField.textColor = .red
但是,如果您也想处理较旧的iOS,这就是我的方法:
创建一个UISearchBar
名为SearchBar : UISearchBar, UISearchBarDelegate
This的自定义类,SearchBar
它将包含UISearchBarDelegate
我要处理的方法及其delegate
设置。
我添加到该类中:
var textField: UITextField? {
if #available(iOS 13.0, *) {
return self.searchTextField
}
return subviews.first?.subviews.first(where: { $0 as? UITextField != nil }) as? UITextField
}
简短说明:
现在,使用iOS13,您可以访问的UITextField
感谢UISearchBar.searchTextField
类型UISearchTextField
,该类型的继承自UITextField
。
因为我知道自己的层次结构,所以我知道我textField
不会使用nill
较旧的版本,因此在两种情况下,我都可以轻松定制一个文本域,适用于从9到13的每个版本。
这是使其工作所需的完整代码:
class SearchBar: UISearchBar, UISearchBarDelegate {
var textField: UITextField? {
if #available(iOS 13.0, *) {
return self.searchTextField
}
return subviews.first?.subviews.first(where: { $0 as? UITextField != nil }) as? UITextField
}
override func awakeFromNib() {
super.awakeFromNib()
delegate = self
if let textField = textField {
textField.textColor = .red
textField.clearButtonMode = .whileEditing
textField.returnKeyType = .search
}
}
}
您还可以SearchBar
通过在上添加一些自定义设置:
let searchBar = UISearchBar.appearance(whenContainedInInstancesOf: [SearchBar.self])
searchBar.backgroundImage = UIImage()
searchBar.isTranslucent = false
searchBar.returnKeyType = .search
然后将其设置到XIB / Storyboard,然后像处理简单一样处理它UISearchBar
(如果您没有忘记委托!)。
(此解决方案仅针对Xcode 10和iOS 12进行了测试。)添加扩展名以访问搜索栏的文本字段:
extension UISearchBar {
var textField: UITextField? {
return subviews.first?.subviews.compactMap { $0 as? UITextField }.first
}
}
然后使用该属性在搜索栏中设置文本字段的文本颜色:
let searchBar = UISearchBar()
searchBar.textField?.textColor = UIColor.white // or whichever color you want
//编辑
上面的代码不适用于iOS13。解决此问题的更好方法是使用:
extension UISearchBar {
var textField: UITextField? {
return self.subviews(ofType: UITextField.self).first
}
}
extension UIView {
var recursiveSubviews: [UIView] {
return self.subviews + self.subviews.flatMap { $0.recursiveSubviews }
}
func subviews<T: UIView>(ofType: T.Type) -> [T] {
return self.recursiveSubviews.compactMap { $0 as? T }
}
}
这是“查找UITextField”方法的Xamarin.iOS C#版本。如果UITextField在将来的iOS视图层次结构中消失,它将不会崩溃。
var tf = searchBar.AllSubViews().FirstOrDefault(v => v is UITextField);
if (tf != null)
(tf as UITextField).TextColor = UIColor.White;
public static IEnumerable<UIView> AllSubViews(this UIView view)
{
foreach (var v in view.Subviews)
{
yield return v;
foreach (var sv in v.AllSubViews())
{
yield return sv;
}
}
}
Swift 5.2和iOS 13.3.1:-
它工作正常。
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
斯威夫特5:
searchBar[keyPath: \.searchTextField].textColor = UIColor(...)