【问题标题】:Changing cursor color on UISearchBar without changing tintColor在不更改 tintColor 的情况下更改 UISearchBar 上的光标颜色
【发布时间】:2014-09-20 09:51:42
【问题描述】:

我希望我的 searchBar 的色调为白色(意味着取消按钮为白色)。当色调为白色时,光标不可见。有没有办法单独设置光标颜色?

【问题讨论】:

标签: ios7 uisearchbar tintcolor


【解决方案1】:

将您的色调颜色设置为您希望取消按钮的颜色,然后使用UIAppearance Protocol 将文本字段上的色调颜色更改为您希望光标的颜色。例如:

[self.searchBar setTintColor:[UIColor whiteColor]];                
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor:[UIColor darkGrayColor]];

【讨论】:

  • 不知道为什么,通过这段代码(第二行),下一个模态场景中 barButton 的颜色也发生了变化。所以我在下面使用了@Felipe 的答案。
  • 我遇到了和 tzaloga 一样的问题。有人知道为什么这会影响模态场景的栏按钮项目颜色吗?
  • 从iOS13开始可以使用searchBar.searchTextField.tintColor
【解决方案2】:

Swift 3.0 和 4 版本

searchController.searchBar.tintColor = .white        
UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .black

请注意,searchBar 不能是可选的。

【讨论】:

    【解决方案3】:

    如果你喜欢 Swift 中功能性但令人讨厌的单行代码,我得到了 Benjamin 的 for 循环:

    searchController.searchBar.tintColor = UIColor.whiteColor()
    
    searchController.searchBar.subviews[0].subviews.flatMap(){ $0 as? UITextField }.first?.tintColor = UIColor.blueColor()
    

    【讨论】:

      【解决方案4】:

      在 Swift 5 中最简单:

          UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black
      

      【讨论】:

      • 太棒了!工作!
      【解决方案5】:

      使用 for-where 语法的紧凑型 Swift 2.0 解决方案(无需中断循环):

      // Make SearchBar's tint color white to get white cancel button.
      searchBar.tintColor = UIColor.white()
      
      // Loop into it's subviews and find TextField, change tint color to something else.
      for subView in searchBar.subviews[0].subviews where subView.isKindOfClass(UITextField) {
              subView.tintColor = UIColor.darkTextColor()
      }
      

      【讨论】:

      • 这对我有用,当我只想为一个 searchBar 更改光标颜色时。
      【解决方案6】:
      searchBar.tintColor = [UIColor whiteColor];
      searchBar.backgroundColor = [UIColor clearColor];
      for ( UIView *v in [searchBar.subviews.firstObject subviews] )
      {
          if ( YES == [v isKindOfClass:[UITextField class]] )
          {
              [((UITextField*)v) setTintColor:[UIColor blueColor]];
              break;
          }
      }
      

      【讨论】:

        【解决方案7】:

        这似乎对我也很有效。

            searchController.searchBar.tintColor = UIColor.whiteColor()
            UITextField.appearanceWhenContainedInInstancesOfClasses([searchController.searchBar.dynamicType]).tintColor = UIColor.blackColor()
        

        【讨论】:

        • Felipe 和 Eric 的解决方案都适合我,但我对 Eric 的解决方案感觉更舒服。
        【解决方案8】:

        对于那些希望在 Swift 中做同样事情的人,这是我在遇到很多麻烦后得出的解决方案:

        override func viewWillAppear(animated: Bool) {
            self.searchBar.tintColor = UIColor.whiteColor()
        
            let view: UIView = self.searchBar.subviews[0] as! UIView
            let subViewsArray = view.subviews
        
            for (subView: UIView) in subViewsArray as! [UIView] {
                println(subView)
                if subView.isKindOfClass(UITextField){
                    subView.tintColor = UIColor.blueColor()
                }
            }
        
        }
        

        【讨论】:

          【解决方案9】:

          我将使用以下代码向 UISearchBar 添加扩展。

          extension UISearchBar {
              var cursorColor: UIColor! {
                  set {
                      for subView in self.subviews[0].subviews where ((subView as? UITextField) != nil) {
                          subView.tintColor = newValue
                      }
                  }
                  get {
                      for subView in self.subviews[0].subviews where ((subView as? UITextField) != nil) {
                          return subView.tintColor
                      }
                      // Return default tintColor
                      return UIColor.eightBit(red: 1, green: 122, blue: 255, alpha: 100)
                  }
              }
          }
          

          【讨论】:

            【解决方案10】:

            iOS 9 及更高版本

            为取消按钮和 textField 设置不同的 tintColor 最简单的方法,使用这个:

            [self.searchBar setTintColor:[UIColor whiteColor]];
            [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:UIColor.blueColor];
            

            【讨论】:

            • 您在第一行的开头缺少 [... 应该是:[self.searchBar setTintColor:[UIColor whiteColor]];
            【解决方案11】:

            这是最简单的解决方案。

            let textField = self.searchBar.value(forKey: "searchField") as! UITextField
            
                textField.tintColor = UIColor.white
            

            【讨论】:

            • let textField = self.searchBar.value(forKey: "searchField") as? UITextField; textField?.tintColor = UIColor.white 避免强制展开,从而避免任何不必要的崩溃。
            【解决方案12】:

            这是 Felipe 答案的“功能”版本 (Swift 4.2)

            // Make SearchBar's tint color white to get white cancel button.
            searchBar.tintColor = .white
            
            // Get the TextField subviews, change tint color to something else.
            if let textFields = searchBar.subviews.first?.subviews.compactMap({ $0 as? UITextField }) {
                    textFields.forEach { $0.tintColor = UIColor.darkGray }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多