Chr*_*örz 93

您必须访问UITextFieldUISearchBar内部.你可以通过使用来做到这一点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)

  • 它不是私有API,但它非常脆弱,可以打破任何iOS更新.你不应该在生产代码中使用这些东西 (4认同)
  • @Christian-发布原始答案的时间并不重要,它仍然依赖于私有API。如果是公开的,则不需要通过KVC访问`searchField`。 (2认同)
  • 投反对票。请参阅@juanjo 的回答和我的评论,以了解 Apple 批准的更强大的方法,并且不太可能中断。 (2认同)
  • 不要使用此答案,您正在访问私有 API,这可能会导致您的应用程序被拒绝。 (2认同)

int*_*upt 52

如果要在故事板(无代码)中为UISearchBar设置文本颜色,也很容易(在身份检查器中).这是搜索栏的红色文字.

在此输入图像描述

  • 视图元素的代码越少越好。谢谢。 (3认同)

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但我想如果你把它放在其他地方也可以.

  • 这是一个比接受答案和其他答案中的键值无意义更有效的方法.然而,Apple的智慧已弃用您在此处使用的API并为iOS 9.0及更高版本提供了新版本,如下所示:`[[UITextField appearanceWhenContainedInInstancesOfClasses:@ [[UISearchBar class]]] setTextColor:[UIColor blueColor]];`这当然是Objective-C,使用`appearance(whenContainedInInstancesOf :)可以明显地转换为Swift. (3认同)
  • 在实际设备上的Xcode 10,iOS 12中,似乎不适用于我。 (2认同)

Sar*_*den 6

从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)


inf*_*oop 5

尝试这个,

searchBar.searchTextField.textColor = .white
Run Code Online (Sandbox Code Playgroud)

我将它与针对 iOS 11 的应用程序一起使用。

[更新] 我观察到,该应用程序在旧版本(< iOS 13)上崩溃,但编译器从未抱怨过版本检查,请有人解释为什么会发生这种情况。