搜索栏色调

时间:2016-04-13 13:13:32

标签: ios swift searchbar tint

在我的应用中,我设置了一个这样的搜索者:

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

此代码部分为我的取消按钮设置了白色文本颜色,为我的搜索字段设置了绿色。

如您所见,光标将为绿色,但文本颜色为黑色。 我的错是什么?

enter image description here

2 个答案:

答案 0 :(得分:3)

你在哪里:

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

更改为:

 if let textView = subView as? UITextField {
      textView.tintColor = UIColor.greenColor()
      textView.textColor = UIColor.greenColor()
 }

答案 1 :(得分:-1)

最快的解决方案是:

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