Can I change the border color of the UISearchDisplayController search bar?

I have a UISearchBar added as a table header, with the following code.

 searchBar = [[UISearchBar alloc] initWithFrame:self.tableView.bounds]; searchBar.tintColor = [UIColor colorWithWhite:185.0/255 alpha:1.0]; [searchBar sizeToFit]; self.tableView.tableHeaderView = searchBar; 

Then I set my UISearchDisplayController up as follows.

 searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; [searchDisplayController setDelegate:self]; [searchDisplayController setSearchResultsDataSource:self]; 

Everything works as we would like, except that the UISearchDisplayController added a blue (ish) border above the search bar - this line does not recognize the tintColor that I set in the search bar.

Can I change the color of this panel? This, obviously, is not entirely important, but this line will scare me forever if it remains blue!

increased by UISearchBar http://nikonizer.yfrog.com/Himg256/scaled.php?tn=0&server=256&filename=4y9.png&xsize=640&ysize=640

+4
source share
3 answers

The border does not seem to change very well with the color of the hue, so my designer insisted that we change it. It's as simple as adding a 1px view at the bottom of the search bar:

Now in viewDidLoad I create a 1px view and position it at the very bottom of our search bar.

 #define SEARCHBAR_BORDER_TAG 1337 - (void) viewDidLoad{ // Set a custom border on the bottom of the search bar, so it not so harsh UISearchBar *searchBar = self.searchDisplayController.searchBar; UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0,searchBar.frame.size.height-1,searchBar.frame.size.width, 1)]; [bottomBorder setBackgroundColor:[UIColor colorWithWhite:200.0f/255.f alpha:1.0f]]; [bottomBorder setOpaque:YES]; [bottomBorder setTag:SEARCHBAR_BORDER_TAG]; [searchBar addSubview:bottomBorder]; [bottomBorder release]; } 

Now I also go to the hue color to the default when the user really searches, because tinting the search bar also changes the color of the cancel button to an ugly color. If you do something like this, the code below will hide / show your border for each state:

 - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{ [controller.searchBar setTintColor:nil]; // Hide our custom border [[controller.searchBar viewWithTag:SEARCHBAR_BORDER_TAG] setHidden:YES]; } - (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller{ [controller.searchBar setTintColor:[UIColor colorWithRed:238.0f/255.0f green:245.0f/255.0f blue:248.0f/255.0f alpha:1.0f]]; //Show our custom border again [[controller.searchBar viewWithTag:SEARCHBAR_BORDER_TAG] setHidden:NO]; } 
+3
source

searchBar.searchBarStyle = UISearchBarStyleMinimal;

+1
source

Try the following:

 searchBar.clipsToBounds = YES; 

Original question link

0
source

Source: https://habr.com/ru/post/1307848/


All Articles