简体   繁体   中英

search bar tint color

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?

在此处输入图片说明

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()
 }

Simplest solution for swift is:

let searchField = searchBar.value(forKey: "searchField") as! UITextField
searchField.tintColor = orangeColor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM