我正在尝试将 UISearchBar
的 barTintColor
设置为不透明。但是,设置 translucent
属性似乎没有任何作用。我在 bare-bones Xcode project here 中重现了这个问题.
self.searchDisplayController.searchBar.translucent = NO;
self.searchDisplayController.searchBar.barTintColor = [UIColor redColor];
上面的红色与 UIViews
中的 [UIColor redColor]
不一样,不是半透明的。我知道涉及在搜索栏上设置背景图片的解决方法,但上面的代码应该也能正常工作。
最佳答案
我下载了你的代码并找到了解决方案,添加一个方法名称是 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];
}
}
}
关于ios - 无法将 UISearchBar 的半透明属性设置为 NO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23556190/