4

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];

除了在搜索栏上方添加了蓝色(ish)边框之外,一切都按照我的意愿运行UISearchDisplayController——这个栏无法识别tintColor我在搜索栏上设置的。

是否可以更改此条的颜色?这显然不是绝对重要的,但如果它保持这样的蓝色,那条线会永远困扰我!

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

4

3 回答 3

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];
}
于 2011-05-20T21:18:20.210 回答
1

searchBar.searchBarStyle = UISearchBarStyleMinimal;

于 2014-07-15T21:38:50.033 回答
0

Try this:

searchBar.clipsToBounds = YES;

Original question link

于 2013-04-08T07:36:16.783 回答