边框似乎没有随着 Tint 颜色的变化很好,所以我的设计师坚持要我们改变它。就像在搜索栏底部添加一个 1px 视图一样简单:
现在在 viewDidLoad 中,我创建了一个 1px 的视图并将其放置在搜索栏的最底部。
#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];
}
现在,当用户实际搜索时,我还将着色颜色转换回默认值,因为着色搜索栏也会将取消按钮颜色着色为难看的颜色。如果你做类似的事情,下面的代码将隐藏/显示每个状态的边框:
- (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];
}