How to change background color of UISearchBar in iOS7

寵の児 提交于 2019-11-27 19:53:40

问题


How to change background color of UISearchBar in iOS7?

not gray, I want to change color like my uinavigationbar

if I Use this code, that's what comes out

searchBar.backgroundColor = [UIColor redColor];

That is not red color!!! This exact same situation as in background color of navigation bar.


回答1:


Need to use:

searchBar.barTintColor = [UIColor redColor];

All thanks!




回答2:


Set the background image to a clear image and you're good to go. This is also pre-ios 7 compatible.

searchBar.backgroundImage = [[UIImage alloc] init]
searchBar.backgroundColor = [UIColor redColor];



回答3:


If the above solutions don't seem working then make sure that you've set the search bar style to Minimal.

[self.searchDisplayController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];

And for simple searchBar

[self.searchBar setSearchBarStyle:UISearchBarStyleMinimal];

SearchBar Style can also be set from interface builder to Minimal.




回答4:


it's not really work for me , or sometimes , if you too , try this

for (UIView *view in [[filterTextField.subviews objectAtIndex:0] subviews]){
                if ([NSStringFromClass([view class]) isEqualToString:@"UISearchBarBackground"])
                    view.alpha = 0;

            }



回答5:


If the UISearchBar was defined in the MainStoryBoard, just click on that UISearchBar and take a look to the options you can handle at right. Over there if you click on the fourth tab (the one that looks like a shield) you've got a Bar Tint option. There you can select the UISearchBar color you want.

If not, I guess programatically you can do something like this:

    UISearchBar* sb =[[UISearchBar alloc] init];
    sb.backgroundColor=[UIColor redColor];

I hope this helps!



来源:https://stackoverflow.com/questions/19301091/how-to-change-background-color-of-uisearchbar-in-ios7

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