我目前正在开发一款在ios7出现之前工作正常的应用程序。搜索栏过去是透明的,并混合到导航栏的蓝色背景中。现在我在ios7中工作,导航栏是蓝色的,但搜索栏有灰色背景。如何将其设为蓝色或透明?
这是一张图片:
答案 0 :(得分:35)
试试这个:
if(IOS_7)
{
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
}
答案 1 :(得分:12)
你可以设置" Bar Tint"到"清除颜色"在Interface Builder(.xib)中:
也可以在代码中完成:
self.searchBar.barTintColor = [UIColor clearColor];
答案 2 :(得分:6)
要使其变为平面颜色,您只需删除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;
答案 3 :(得分:0)
Swift 4.2
您可以使用此扩展名来更改SearchBar的字体和背景颜色。
#include <string>
#include <iostream>
std::string &&f(std::string &&a) {
return std::move(a);
}
int main() {
std::string &&a = f("lol");
std::cout << a << std::endl;
return 0;
}
答案 4 :(得分:-1)
**编辑 - 这在iOS 7中适用于我
// Set the color to whatever blue color that is in your screenshot
self.searchBar.backgroundImage = [UIImage imageWithColor:[UIColor redColor] cornerRadius:5.0f];
如果您希望所有搜索栏都是某种颜色,请执行以下操作:
// Put this in your app delegate's didFinishLaunchingWithOptions method
// Whatever color you want for searchBarColor
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { // For iOS 7
UIColor *searchBarColor = [UIColor blueColor];
[[UISearchBar appearance] setBackgroundColor:searchBarColor];
}
如果您只是希望特定搜索栏背景为颜色:
// Set it in your viewDidLoad method of your controller
// Replace the yourSearchBar property with whatever you're doing to instantiate the search bar
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { // For iOS 7
{
UIColor *searchBarColor = [UIColor blueColor];
self.yourSearchBar.backgroundColor = searchBarColor;
}