如何将纯色应用于UISearchBar

She*_*lam 2 iphone cocoa-touch objective-c uisearchbar

我在SO上看过这篇文章:UISearchBar纯色

但没有提供答案.

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

Ter*_*ard 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
Run Code Online (Sandbox Code Playgroud)