Reputation: 11113
I am trying to set the barTintColor
of a UISearchBar
without translucency. However, setting the translucent
property doesn't seem to do anything. I have reproduced the issue in a bare-bones Xcode project here.
self.searchDisplayController.searchBar.translucent = NO;
self.searchDisplayController.searchBar.barTintColor = [UIColor redColor];
The red color above is not the same as [UIColor redColor]
in UIViews
that are not translucent. I know about the workaround involving setting a background image on the search bar, but the above code should work as well.
Upvotes: 4
Views: 754
Reputation: 8117
Maybe too late for the party,
However I've done a very simple and nifty extension for Swift 3 that allows you to use the translucent property without any trouble.
It involves no use of private API or dangerous walk-through within the object.
You can download it from this Github repository.
Upvotes: 0
Reputation: 1918
Please set image in search bar like this it will solve your problem.
[[UISearchBar appearance] setBackgroundImage:[UIImage imageNamed:@"red"]];
Thanks.
Upvotes: 0
Reputation: 47059
I downloaded your code and find solution for, add one method name is removeUISearchBarBackgroundInViewHierarchy
and set searchBar.backgroundColor
as 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];
}
}
}
Upvotes: 4