【问题标题】:Is it possible to change the border color of a UISearchDisplayController's search bar?是否可以更改 UISearchDisplayController 搜索栏的边框颜色?
【发布时间】:2010-04-24 01:34:18
【问题描述】:

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

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

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

【问题讨论】:

    标签: iphone iphone-sdk-3.0 uisearchbar uisearchdisplaycontroller


    【解决方案1】:

    边框似乎没有随着 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];
    }
    

    【讨论】:

    • 不错的技术,但不幸的是,它是导致我出现问题的顶部边框,它实际上超出了 UISearchBar 的范围,不能被这样的子视图覆盖。
    【解决方案2】:

    searchBar.searchBarStyle = UISearchBarStyleMinimal;

    【讨论】:

      【解决方案3】:

      试试这个:

      searchBar.clipsToBounds = YES;
      

      原问题link

      【讨论】:

        猜你喜欢
        • 2011-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-20
        • 2019-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多