是否可以更改UISearchDisplayController搜索栏的边框颜色?

时间:2010-04-24 01:34:19

标签: iphone iphone-sdk-3.0 uisearchbar uisearchdisplaycontroller

我添加了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>

是否可以更改此栏的颜色?这显然不是绝对至关重要的,但如果它像那样保持蓝色,那条线将永远让我感到烦恼!

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

3 个答案:

答案 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)

试试这个:

searchBar.clipsToBounds = YES;

原始问题link