UISearchBar change placeholder color

前端 未结 17 2410
南旧
南旧 2020-12-04 15:16

Has anyone any idea or code sample on how can I change the text color of the placeholder text of a UISearchBar?

17条回答
  •  [愿得一人]
    2020-12-04 16:04

    This solution works on Xcode 8.2.1. with Swift 3.0. :

    extension UISearchBar
    {
        func setPlaceholderTextColorTo(color: UIColor)
        {
            let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
            textFieldInsideSearchBar?.textColor = color
            let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
            textFieldInsideSearchBarLabel?.textColor = color
        }
    }
    

    Usage example:

    searchController.searchBar.setPlaceholderTextColorTo(color: mainColor)
    

提交回复
热议问题