0

In my app I have set a searcher like this:

searchBar.delegate = self
searchBar.showsCancelButton = true
searchBar.tintColor = UIColor.whiteColor()
searchBar.spellCheckingType = .No
searchBar.autocapitalizationType = .None
searchBar.autocorrectionType = .No
searchBar.returnKeyType = .Done
searchBar.sizeToFit()

let view: UIView = self.searchBar.subviews[0] as UIView
   for subView: UIView in view.subviews {
      if subView.isKindOfClass(UITextField){
         subView.tintColor = UIColor.greenColor()
         }
      }

this code part set an white text color for my cancel button and a green color for my search field.

as you can see, the cursor will be green, but the text color is black. what is my mistake?

enter image description here

Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
Stack108
  • 915
  • 2
  • 14
  • 35

2 Answers2

5

Where you have:

 if subView.isKindOfClass(UITextField){
      subView.tintColor = UIColor.greenColor()
 }

Change to:

 if let textView = subView as? UITextField {
      textView.tintColor = UIColor.greenColor()
      textView.textColor = UIColor.greenColor()
 }
Sean Lintern
  • 3,103
  • 22
  • 28
0

Simplest solution for swift is:

let searchField = searchBar.value(forKey: "searchField") as! UITextField
searchField.tintColor = orangeColor
Gulfam Khan
  • 1,010
  • 12
  • 23
  • I am not going to criticise anyone to giving me a downvote. If you found something wrong then it's rightful to do. I just want to request that, please do not downvote only but also mention the reason in comment. So, he/she would not repeat the same mistake again. Thanks – Gulfam Khan Feb 09 '19 at 13:05