objective-c 如何在 iOS7 中更改 UISearchBar 的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19301091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change background color of UISearchBar in iOS7
提问by dev.nikolaz
How to change background color of UISearchBar in iOS7?
如何在 iOS7 中更改 UISearchBar 的背景颜色?


not gray, I want to change color like my uinavigationbar
不是灰色,我想像我的 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.
那不是红色!!!这与导航栏的背景颜色完全相同。
回答by dev.nikolaz
Need to use:
需要使用:
searchBar.barTintColor = [UIColor redColor];


All thanks!
所有的感谢!
回答by Vadoff
Set the background image to a clear image and you're good to go. This is also pre-ios 7 compatible.
将背景图像设置为清晰的图像,您就可以开始了。这也与 ios 7 之前的版本兼容。
searchBar.backgroundImage = [[UIImage alloc] init]
searchBar.backgroundColor = [UIColor redColor];
回答by zeeawan
If the above solutions don't seem working then make sure that you've set the search bar style to Minimal.
如果上述解决方案似乎不起作用,请确保您已将搜索栏样式设置为Minimal。
[self.searchDisplayController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
And for simple searchBar
对于简单的搜索栏
[self.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
SearchBar Stylecan also be set from interface builder to Minimal.
SearchBar 样式也可以从界面构建器设置为Minimal。
回答by chings228
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;
}
回答by FabKremer
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.
如果 UISearchBar 是在 MainStoryBoard 中定义的,只需单击该 UISearchBar 并查看右侧可以处理的选项。在那里,如果您单击第四个选项卡(看起来像盾牌的选项卡),您将获得一个 Bar Tint 选项。在那里你可以选择你想要的 UISearchBar 颜色。
If not, I guess programatically you can do something like this:
如果没有,我想以编程方式您可以执行以下操作:
UISearchBar* sb =[[UISearchBar alloc] init];
sb.backgroundColor=[UIColor redColor];
I hope this helps!
我希望这有帮助!

