I have a UISearchBar
added as my 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];
Ev开发者_如何学Cerything functions as I would like except that the UISearchDisplayController
has added a blue(ish) border above the search bar — this bar does not recognise the tintColor
I have set on the search bar.
Is it possible to change the color of this bar? It is obviously not absolutely crucial but that line is going to bug me forever if it stays blue like that!
zoomed in on UISearchBar http://nikonizer.yfrog.com/Himg256/scaled.php?tn=0&server=256&filename=4y9.png&xsize=640&ysize=640
The border doesn't seem to change very well with the Tint color, so my designer insisted that we change it. It's as simple as adding a 1px view to the bottom of your search bar:
Now in the 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's 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 transition the tint color back to default when the user is actually searching, because tinting the search bar also tints the cancel button color to an ugly color. If you do something similar, 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];
}
searchBar.searchBarStyle = UISearchBarStyleMinimal;
Try this:
searchBar.clipsToBounds = YES;
Original question link
精彩评论