我正在尝试设置barTintColor
UISearchBar
而不是半透明。但是,设置translucent
属性似乎没有做任何事情。我已在bare-bones Xcode project here中重现了该问题。
self.searchDisplayController.searchBar.translucent = NO;
self.searchDisplayController.searchBar.barTintColor = [UIColor redColor];
上面的红色与[UIColor redColor]
中不是半透明的UIViews
不一样。我知道涉及在搜索栏上设置背景图像的解决方法,但上面的代码也可以正常工作。
答案 0 :(得分:4)
我下载了您的代码并找到了解决方案,添加了一个方法名称removeUISearchBarBackgroundInViewHierarchy
并将searchBar.backgroundColor
设置为redColor.
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchDisplayController.searchBar.translucent = NO;
[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.backgroundColor = [UIColor redColor];
}
- (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];
}
}
}
答案 1 :(得分:0)
请在搜索栏中设置图像,这样可以解决您的问题。
[[UISearchBar appearance] setBackgroundImage:[UIImage imageNamed:@"red"]];
感谢。
答案 2 :(得分:0)
派对可能为时已晚,
然而,我为Swift 3做了一个非常简单和漂亮的扩展,允许您毫无困难地使用半透明属性。
它不涉及在对象内使用私有API或危险的漫游。
您可以从此Github repository下载。