We have a UITableView
with a searchbar
added with the searchDisplayController
.
We want to have translucency off throughout the app.
I have the translucency off for the navigation bar and other bars, but not the search bar when it uses the display controller. In one part of the app when we use the search bar but not the display controller, the translucency is set correctly.
How can I set the translucent property of the UISearchBar
with the display controller to be NO?
EDIT:
this is my code in viewDidLoad
self.navigationController.navigationBar.translucent = NO;
BOOL t = self.searchDisplayController.searchBar.translucent;
self.searchDisplayController.searchBar.translucent = NO;
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.searchDisplayController.searchBar.barTintColor = [UIColor redColor];
UIBarStyle b1 = self.searchDisplayController.searchBar.barStyle;
UISearchBarStyle b2 = self.searchDisplayController.searchBar.searchBarStyle;
BOOL t2 = self.searchDisplayController.searchBar.translucent;
Running in the debugger, t = YES and t2 = YES. b1 = UIBarStyleDefault
and b2 = UISearchBarStyleDefault
. Am I setting NO at the wrong spot? Ive tried the setting in the storyboard
and here in viewDidLoad
For UISearchBarStyleProminent:
1) Be sure to check the "Translucent" box for the search bar in the Attributes Inspector.
2) Add the following to viewDidLoad:
self.navigationController.navigationBar.translucent = NO; // If you have a navBar
self.searchDisplayController.searchBar.translucent = NO;
Edit From @RudolfAdamkovic:
"I've found that for
UISearchBarStyleProminent
, executing [the following] helps. That way, you can keep it on in Storyboard."searchBar.translucent = YES;
searchBar.translucent = NO;
For UISearchBarStyleMinimal:
In order to get the minimal search bar to not be translucent I have put together a workaround.
1) Be sure to check the "Translucent" box for the search bar in the Attributes Inspector.
2) Add the following code to viewDidLoad:
self.navigationController.navigationBar.translucent = NO;
self.searchDisplayController.searchBar.translucent = NO;
self.searchDisplayController.searchBar.backgroundColor = [UIColor desiredColor];
3) A UIView needs to be added to the viewController. This view needs to be 20px heigh and should have the same color as the searchBar.barTintColor.
Note: I think this workaround is needed because: "The style UISearchBarStyleMinimal provides no default background color or image but will display one if customized as such." Thus the only way to get this functionality for UISearchBarStyleMinimal is to set the backgroundColor.
See UISearchBar documentation for more details.
None of the above answers worked for me on iOS 7/8. Here's some setup code that did the trick:
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)];
searchBar.scopeButtonTitles = @[@"Scope1", @"Scope2"];
searchBar.selectedScopeButtonIndex = 0;
searchBar.backgroundColor = [UIColor clearColor];
searchBar.barTintColor = [UIColor clearColor];
searchBar.translucent = YES; // SUPER IMPORTANT, REMOVING THIS MESSED UP THE SCOPE BAR
// ONLY USE IMAGES, NOT BACKGROUND COLORS
UIImage *searchBarBackgroundImage = [[UIImage imageNamed:@"SearchBarBackgroundImage"];
UIImage *scopeBarBackgroundImage = [[UIImage imageNamed:@"ScopeBarBackgroundImage"];
[searchBar setBackgroundImage:searchBarBackgroundImage
forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
searchBar.scopeBarBackgroundImage = scopeBarBackgroundImage;
searchBar.tintColor = [UIColor whiteColor];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With