如何改变搜索栏的颜色?(How to change color of Search Bar?)

2019-09-17 03:09发布

我已经实现了一个搜索栏,我想改变搜索栏的颜色。 我怎样才能做到这一点?

我试着:

self.mySearchBar.backgroundColor = [UIColor redColor];

但没有成功。

UPDATE(解决):

我收到我定义为以下代码背景颜色删除默认的背景色。

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];

Answer 1:

尝试:

self.mySearchBar.barTintColor = [UIColor redColor];


Answer 2:

self.mySearchBar.backgroundColor = [UIColor redColor]; 

什么都不会做,

self.mySearchBar.tintColor = [UIColor redColor]; 

将!



Answer 3:

    self.mySearchBar.backgroundVolor = [UIColor redColor];

“Volor”或“颜色

  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或风格,只是做在厦门国际银行,它将被加载到应用程序。

但唯一的真正的方式来改变背景颜色覆盖它,看看回答以下问题

明确的UISearchBar背景颜色或设置背景图像[iPhone的SDK]



Answer 4:

我不知道是否因为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;


Answer 5:

searchBar.barTintColor =  UIColor.redColor()


Answer 6:

试试这个代码,它工作正常,我。

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


Answer 7:

如果你有收入和利润的问题尽量去除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];


Answer 8:

我必须设置的UISearchBar的searchBarStyle.default ,否则没有什么作品。



文章来源: How to change color of Search Bar?