我添加了UISearchBar
作为我的表格标题,其中包含以下代码。
searchBar = [[UISearchBar alloc] initWithFrame:self.tableView.bounds];
searchBar.tintColor = [UIColor colorWithWhite:185.0/255 alpha:1.0];
[searchBar sizeToFit];
self.tableView.tableHeaderView = searchBar;
然后我将UISearchDisplayController
设置如下。
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
[searchDisplayController setDelegate:self];
[searchDisplayController setSearchResultsDataSource:self];
除了UISearchDisplayController
在搜索栏上方添加了蓝色(ish)边框外,所有功能都按照我的意愿运行 - 此栏无法识别我在搜索栏上设置的tintColor
。< / p>
是否可以更改此栏的颜色?这显然不是绝对至关重要的,但如果它像那样保持蓝色,那条线将永远让我感到烦恼!
答案 0 :(得分:3)
边框似乎没有很好地改变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];
}
答案 1 :(得分:1)
searchBar.searchBarStyle = UISearchBarStyleMinimal;
答案 2 :(得分:0)