如何将纯色应用于UISearchBar

时间:2010-08-16 19:56:07

标签: iphone objective-c cocoa-touch uisearchbar

我在SO上看过这篇文章:UISearchBar solid color

但没有提供答案。

我想知道如何将纯色应用于UISearchBar。我已经应用了tintColor但它会自动创建一个渐变。

3 个答案:

答案 0 :(得分:15)

您可以覆盖搜索栏的drawRect。通过将“UISearchBarBackground”视图设置为0的alpha,它将绘制您设置的扁平色调颜色。例如,在您的App Delegate中添加以下内容:

@implementation UISearchBar( CustomBackground )
- (void)drawRect:(CGRect)rect
{
    // Find the UISearchBarBackground view and set it's alpha to 0 so we get a "flat"
    // search bar.
    for( UIView *subview in self.subviews )
    {
        if( [subview isKindOfClass:NSClassFromString( @"UISearchBarBackground" )] )
        {
            [subview setAlpha:0.0F];
            break;
        }
    }
}
@end

答案 1 :(得分:2)

        _tempSearchBar =[[UISearchBar alloc]initWithFrame:CGRectMake(44, 0, 320 - 44, 43)];
        _tempSearchBar.barStyle=UIBarStyleDefault;
        _tempSearchBar.placeholder=@"搜索";
        for( UIView *subview in _tempSearchBar.subviews )
        {
            if( [subview isKindOfClass:NSClassFromString( @"UISearchBarBackground" )] )
            {
                UIView *aView = [[UIView alloc] initWithFrame:subview.bounds];
                aView.backgroundColor = [UIColor greenColor];
//                [subview setAlpha:0.0f];
                [subview addSubview:aView];
                break;
            }
        }

答案 2 :(得分:0)

我这样做了:

[toolbar setTintColor:[UIColor greenColor]];

得到了这个:

alt text http://gallery.me.com/davedelong/100084/Screen%20shot%202010-08-16%20at%202.44.07%20PM/web.png?ver=12819951050001

这就是你要找的东西吗?