【问题标题】:How to change color of Search Bar?如何更改搜索栏的颜色?
【发布时间】:2012-05-18 23:11:22
【问题描述】:

我已经实现了一个搜索栏,我想更改搜索栏的颜色。我该怎么做?

我试过了:

self.mySearchBar.backgroundColor = [UIColor redColor];

但没有成功。

更新(已解决):

在使用以下代码定义背景颜色之前,我必须删除默认背景颜色。

for (UIView *subview in mySearchBar.subviews) {
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [subview removeFromSuperview];
        break;
    }
} 

...所以代码将是下一个:

  for (UIView *subview in mySearchBar.subviews) {
      if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
          [subview removeFromSuperview];
          break;
      }
  } 

  self.mySearchBar.backgroundColor = [UIColor redColor];

【问题讨论】:

标签: iphone objective-c xcode uisearchbar


【解决方案1】:

取决于你到底要改变什么

searchBar.barTintColor
searchBar.tintColor

您可以自定义其他项目,包括一些额外的代码

【讨论】:

    【解决方案2】:

    我必须将 UISearchBar 的 searchBarStyle 设置为 .default,否则无法正常工作。

    【讨论】:

      【解决方案3】:

      如果您对顶线和底线有疑问,请尝试删除 UISearchBarBackground:

      self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
      
      for (UIView *subview in self.searchBar.subviews) {
          if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
              [subview removeFromSuperview];
              break;
          }
          for (UIView *subsubview in subview.subviews) {
              if ([subsubview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
                  [subsubview removeFromSuperview];
                  break;
              }
          }
      }
      
      self.searchBar.barTintColor = [UIColor redColor];
      self.searchBar.backgroundColor = [UIColor redColor];
      

      【讨论】:

        【解决方案4】:

        试试:

        self.mySearchBar.barTintColor = [UIColor redColor];
        

        【讨论】:

          【解决方案5】:
          searchBar.barTintColor =  UIColor.redColor()
          

          【讨论】:

          • 最好详细说明为什么这是正确答案 - 围绕代码 sn-p 提供一些上下文。
          【解决方案6】:

          我不确定它自 iOS7 以来是否发生了变化,但似乎搜索显示控制器中的搜索栏需要一个额外的循环才能正确删除 UISearchBarBackground 视图。

          我创建了一个递归方法来正确清理搜索栏。

          - (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view
          {
              for (UIView *subview in [view subviews]) {
                  if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
                      [subview removeFromSuperview];
                      break; //To avoid an extra loop as there is only one UISearchBarBackground
                  } else {
                      [self removeUISearchBarBackgroundInViewHierarchy:subview];
                  }
              }
          }
          

          您可以简单地将搜索栏发送到该方法,然后更改颜色,有点像上面建议的答案。

          [self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
          self.searchDisplayController.searchBar.backgroundColor = yourUIColor;
          

          【讨论】:

            【解决方案7】:
            self.mySearchBar.backgroundColor = [UIColor redColor]; 
            

            什么都不做,

            但是

            self.mySearchBar.tintColor = [UIColor redColor]; 
            

            会的!

            【讨论】:

              【解决方案8】:

              试试这个代码,它对我来说很好。

              for( UIView* v in [parent subviews] ) { 
              if( [v isKindOfClass:[UISearchBar class]] ) {
                [(UISearchBar*)v  setTintColor:[UIColor blackColor]];
              }
              

              【讨论】:

                【解决方案9】:
                    self.mySearchBar.backgroundVolor = [UIColor redColor];
                

                “Volor”或“Color”

                  self.mySearchBar.backgroundColor = [UIColor redColor];
                

                如果你想给它上色:

                根据文档:https://developer.apple.com/iphone/library/documentation/UIKit/Reference/UISearchBar_Class/Reference/Reference.html,UISearchBar 类上有一个 tintColor 属性。

                在 TableSearch 示例中:https://developer.apple.com/library/ios/#samplecode/TableSearch/ 搜索栏在 MainView.xib 中定义和加载。如果你想改变它的 tintColor 或样式,只需在 xib 中进行,它就会被加载到应用程序中。

                但更改背景颜色的唯一真正方法是覆盖它,请查看以下问题的答案

                UISearchBar clear background color or set background image [iphone sdk]

                【讨论】:

                • 这是什么意思? “backgroundVolor”还是“backgroundColor”?
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-02-06
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多