【问题标题】:UISearchBar cancel button color?UISearchBar 取消按钮颜色?
【发布时间】:2011-02-16 18:22:41
【问题描述】:

当我将 UISearchBar 拖放到 Interface Builder 中的视图中,并将其样式更改为黑色不透明时,取消按钮会保持不合适的蓝色/灰色并且不会变成黑色。

如何让取消按钮变黑?

编辑:它确实像这样工作:

// Assume a UISearchBar searchBar.
NSArray *subviews = [searchBar subviews];

// The index depends on how you configure the searchBar.
UIButton *cancelButton = [subviews objectAtIndex:3];

// Set the style to "normal" style.
[cancelButton setStyle:0];

setStyle: 方法来自私有框架,因此在将应用程序提交给 Apple 时这可能是一个问题。

【问题讨论】:

  • 而不是使用 [canelButton setStyle:0] 使用 cancelButton.tintColor=[UIColor blackColor];

标签: ios cocoa-touch uisearchbar background-color uibarbuttonitem


【解决方案1】:

我用过这样的东西并和我一起工作:

[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor blackColor]];

它将取消按钮的颜色更改为黑色。

更新 iOS 9.0,appearanceWhenContainedIn 方法已弃用,改用appearanceWhenContainedInInstancesOfClasses

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor blackColor]];

在 Swift 3 中:

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

【讨论】:

  • 在 iOS 9 中已弃用:apple doc。解决方案:stackoverflow.com/questions/24136874/…
  • 不适用于 iOS 9。按钮颜色保持不变。
  • 这种疯狂的投票是怎么回事?设置tintColor,工作完成
  • @AdamWaite tintColor 更改光标和“取消”按钮的颜色。
  • @ChaitanyaShah 只需将 searchBar tintColor 设置为所需的“取消”按钮颜色,并将 UITextField 的外观当ContainedIn... 设置为另一种颜色
【解决方案2】:

您的解决方案的问题是代码假设 objectAtIndex:3 是取消按钮。这不仅会生成编译器警告,而且如果您以编程方式显示“取消”按钮(例如使用 [searchBar setShowsCancelButton:YES],则可能会导致应用程序崩溃。

一个更简单的解决方案是在 ViewDidLoad() 中设置整个搜索栏的样式,使用:

searchBar.tintColor = [UIColor colorWithWhite:0.3 alpha:1.0];

这会覆盖在 Interface Builder 中设置的样式,但也会将 Cancel 按钮的颜色更改为与整个栏相同的颜色(尽管它不允许您独立设置 Cancel 按钮的样式,很遗憾。

【讨论】:

  • 我们也可以独立地改变取消按钮的颜色,而不考虑搜索栏的颜色。 searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(10,346,300,0)]; searchBar.tintColor=[Reusables SKColorFromHexString:@"#505a62"]; searchBar.delegate=自己; searchBar.showsCancelButton=YES; NSArray *subviews = [searchBar 子视图]; // 索引取决于你如何配置 searchBar。 UIButton *cancelButton = [子视图 objectAtIndex:2]; cancelButton.tintColor=[UIColor lightTextColor];
【解决方案3】:

试试这个看看:(我用 Swift 4.1 测试了下面的代码 - Xcode 9.3-beta4

@IBOutlet weak var sbSearchBar: UISearchBar!

sbSearchBar.showsCancelButton = true
sbSearchBar.barTintColor = UIColor.blue
sbSearchBar.tintColor = UIColor.red

if let buttonItem = sbSearchBar.subviews.first?.subviews.last as? UIButton {
    buttonItem.setTitleColor(UIColor.yellow, for: .normal)
}

【讨论】:

  • 嗨,我认为你应该使用first(where:) 找到UINavigationButton ``` if let cancelItem = searchBar.subviews.first?.subviews.first(where: { $0 is UIButton })作为? UIButton { cancelItem.setTitleColor(UIColor.black, for: .normal) } ```
【解决方案4】:

在 Swift 4.2 中

let appearance = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
appearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(named: "goldColor")!], for: .normal)

这对我有用。谢谢@Tim Semple

【讨论】:

    【解决方案5】:

    对于 iOS 10:

    UISearchBar.appearance().tintColor = UIColor.red //cancel button color
    UISearchBar.appearance().barTintColor = UIColor.blue //background button color
    

    【讨论】:

    • 把它放在AppDelegate.application() 中会为你的应用设置全局,这对我来说是完美的。简单有效。
    【解决方案6】:

    这是 Swift 3 的 Hossam Ghareeb's answer above 的更新版本:

    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self] ).tintColor = UIColor.red
    

    但是,如果它已经在别处为 UIBarButtonItem 设置,这不会覆盖外观。

    例如,在我的导航栏控制器中,我必须更改:

    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: UIControlState.normal)
    

    为了使上述解决方案起作用:

    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self] ).setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: UIControlState.normal)
    

    【讨论】:

      【解决方案7】:

      想出了以下解决方案,它也适用于 iOS 13.0 和 iOS 12.4,必须适用于 iOS 9.0 之前的早期版本。以下解决方案适用于:

      1. 取消按钮颜色(正常状态)。
      2. 取消按钮颜色(禁用状态)。
      3. 搜索栏文本字段背景颜色(正常状态)。

      对于目标 C:

      [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor whiteColor]]; 
      
      [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
      
      [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateDisabled];
      
      [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{NSBackgroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
      

      上面的代码还修复了我在 iOS 13 和 iPhone X 上的 UI 问题。 我将此代码包含在 didFinishLaunchingWithOptions 函数的 AppDelegate.m 类中,以便可以在整个应用程序中完成更改。

      【讨论】:

        【解决方案8】:

        我采用了 Benjamin 的 答案并将其与安全的 Array 查找相结合,以生成一个简短但安全的功能 版本:

        searchController.searchBar.tintColor = UIColor.whiteColor()
        (searchController.searchBar.subviews[safe: 0]?.subviews as? [UIView])?
            .filter({$0.isKindOfClass(UITextField)})
            .map({$0.tintColor = .lightGrayColor()})
        

        这会导致在键入时将“取消”按钮着色为白色,并将光标着色为灰色。否则它将是白色的,因此看不到。 searchControllerUISearchController 类型的对象。如果有人想在结果控制器中使用它,请将其替换为 self

        safe:下标的实现是nkukushkin's答案:

        extension Array {
            subscript(safe index: Int) -> T? {
                return indices(self) ~= index ? self[index] : nil
            }
        }
        

        【讨论】:

          【解决方案9】:

          点击搜索栏并在 Interface Builder 的视图下设置 tint 颜色。

          【讨论】:

            【解决方案10】:
            let view: UIView = self.searchBar.subviews[0] as UIView
            let subViewsArray = view.subviews
            
            for subView: UIView in subViewsArray {
                if let cancelButt = subView as? UIButton{
                    cancelButt.setTitleColor(UIColor.white, for: .normal)         
                }
            }
            

            这对我有用

            【讨论】:

              【解决方案11】:

              对于那些希望在 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()
                      }
                  }
              
              }
              

              【讨论】:

                猜你喜欢
                • 2014-10-23
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-01-19
                • 1970-01-01
                相关资源
                最近更新 更多