Chr*_*örz 93
您必须访问UITextField
UISearchBar内部.你可以通过使用来做到这一点valueForKey("searchField")
var textFieldInsideSearchBar = yourSearchbar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar?.textColor = yourcolor
Run Code Online (Sandbox Code Playgroud)
Swift 3更新
let textFieldInsideSearchBar = yourSearchbar.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.textColor = yourcolor
Run Code Online (Sandbox Code Playgroud)
chr*_*kum 43
在我的Swift 4工作:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
Run Code Online (Sandbox Code Playgroud)
Swift 4.2,IOS 12:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
Run Code Online (Sandbox Code Playgroud)
小智 40
如果您只需要在深色背景上使其可读,您可以更改barStyle.以下内容使搜索栏中的文本和按钮变为白色:
searchController.searchBar.barStyle = .black
Run Code Online (Sandbox Code Playgroud)
Ada*_*ite 30
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
}
}
Run Code Online (Sandbox Code Playgroud)
Tal*_*ion 10
我认为最方便的方法是设置textColor
属性UISearchBar
.
Swift 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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法
searchBar.textColor = UIColor.blue // Your color
Run Code Online (Sandbox Code Playgroud)
斯威夫特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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你会用它作为:
searchBar.textColor = UIColor.blueColor() // Your color
Run Code Online (Sandbox Code Playgroud)
jua*_*njo 10
这适用于iOS 11,Xcode 9:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).textColor = UIColor.blue
Run Code Online (Sandbox Code Playgroud)
我把它写在了,AppDelegate
但我想如果你把它放在其他地方也可以.
从Xcode 11和iOS 13开始,可以直接访问文本字段:
searchBar.searchTextField
Run Code Online (Sandbox Code Playgroud)
您可以编写一些代码以使其始终可以像这样访问。
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
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以将其设置为带有致命错误的非可选选项,此代码自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")
}
}
Run Code Online (Sandbox Code Playgroud)
尝试这个,
searchBar.searchTextField.textColor = .white
Run Code Online (Sandbox Code Playgroud)
我将它与针对 iOS 11 的应用程序一起使用。
[更新] 我观察到,该应用程序在旧版本(< iOS 13)上崩溃,但编译器从未抱怨过版本检查,请有人解释为什么会发生这种情况。
归档时间: |
|
查看次数: |
43667 次 |
最近记录: |