UISearchBar change placeholder color

前端 未结 17 2421
南旧
南旧 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 15:49

    Swift 5 - ios 13:

    Those who are stuck let me tell you it is going to work only in viewDidLayoutSubviews not in viewDidLoad

    override func viewDidLayoutSubviews() {
    
        setupSearchBar(searchBar: YourSearchBar)
    
    }
    
        func setupSearchBar(searchBar : UISearchBar) {
    
        searchBar.setPlaceholderTextColorTo(color: UIColor.white)        
    
       }
    
    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
        }
    }
    

    Happy coding :)

提交回复
热议问题