How to change color of Search Bar?

自闭症网瘾萝莉.ら 提交于 2019-12-10 14:14:25

问题


I've implemented a Search Bar and I want to change color of Search Bar. How can I do that?

I've tried with:

self.mySearchBar.backgroundColor = [UIColor redColor];

but with no success.

UPDATE (Solved):

I had to remove the default background color before I defined background color with the following code.

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

... so code would be next:

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

  self.mySearchBar.backgroundColor = [UIColor redColor];

回答1:


Try:

self.mySearchBar.barTintColor = [UIColor redColor];



回答2:


self.mySearchBar.backgroundColor = [UIColor redColor]; 

won't do anything,

but

self.mySearchBar.tintColor = [UIColor redColor]; 

will!




回答3:


    self.mySearchBar.backgroundVolor = [UIColor redColor];

"Volor" or "Color

  self.mySearchBar.backgroundColor = [UIColor redColor];

Also if you want to tint it:

According to the documentation : https://developer.apple.com/iphone/library/documentation/UIKit/Reference/UISearchBar_Class/Reference/Reference.html, there is a tintColor property on the UISearchBar class.

In the TableSearch example: https://developer.apple.com/library/ios/#samplecode/TableSearch/ the search bar is defined and loaded in the MainView.xib. If you want to change its tintColor or style, just do it in the xib and it will be loaded into the application.

BUT THE ONLY REAL WAY to change background color is to override it, look at the answer from the following question

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




回答4:


I'm not sure if it has changed since iOS7, but it seems that the search bar in the search display controller requires an extra loop to properly remove the UISearchBarBackground view.

I created a recursive method to properly clean the search bar.

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

You can simply send your search bar to the method and change the color afterward, a bit like the suggested answers above.

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



回答5:


I had to set UISearchBar's searchBarStyle to .default, otherwise nothing works.




回答6:


searchBar.barTintColor =  UIColor.redColor()



回答7:


Try this code it works fine for me.

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



回答8:


If you have issues with top and bottom lines try to remove the 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];


来源:https://stackoverflow.com/questions/10532995/how-to-change-color-of-search-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!