Set translucent NO to UISearchBar

We have a UITableView with searchbar with searchDisplayController .

We want transparency to be absent throughout the application.

I have transparency for the navigation bar and other bars, but not the search bar when it uses the display controller. In one part of the application, when we use the search bar, but not the display controller, transparency is set correctly.

How to set value of translucent property of UISearchBar with NO display controller?

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; 

Run in the debugger, t = YES and t2 = YES. b1 = UIBarStyleDefault and b2 = UISearchBarStyleDefault . Am I installing NO in the wrong place? Ive tried installing in storyboard and here in viewDidLoad

+4
source share
2 answers

For UISearchBarStyleProminent:

1) Be sure to check the Transparent field 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 found that for UISearchBarStyleProminent helps [the following]. That way you can save it in the storyboard."
searchBar.translucent = YES;
searchBar.translucent = NO;

For UISearchBarStyleMinimal:

So that the minimum search string is not translucent, I put together a workaround.

1) Be sure to check the Transparent field for the search bar in the Attributes Inspector.

2) Add the following code to view DidLoad:

 self.navigationController.navigationBar.translucent = NO; self.searchDisplayController.searchBar.translucent = NO; self.searchDisplayController.searchBar.backgroundColor = [UIColor desiredColor]; 

3) UIView needs to be added to viewController. This view should be 20px heigh and should be the same color as searchBar.barTintColor.

Note: I think this workaround is necessary because: "The UISearchBarStyleMinimal style does not support the background color or image by default, but will be displayed alone if it is configured as such." So the only way to get this function for UISearchBarStyleMinimal is to set backgroundColor.

See the UISearchBar documentation for more details.

+6
source

None of the above answers worked for me on iOS 7/8. Here is the 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]; 
+1
source

All Articles