UISearchBar barColor与UINavigationBar不匹配

use*_*418 2 objective-c uinavigationbar uicolor uisearchbar ios

我将barColor我的UISearchBar设置为导航栏设置的颜色.当我在UI中返回结果时颜色不匹配.我将搜索栏和导航栏的半透明设置为YES.

在此输入图像描述 在此输入图像描述

任何人都可以告诉我如何获得搜索栏的颜色以匹配导航栏颜色?

Dim*_*ima 6

我发现实现这一目标的最简单方法是使用图像而不仅仅是颜色.

[mySearchBar setBackgroundImage:[UIImage imageWithColor:[UIColor blackColor] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

imageWithColor:是我写的类别方法UIImage,如下所示:

+ (UIImage *) imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
Run Code Online (Sandbox Code Playgroud)

这样做,我能够得到完全UISearchBar匹配UINavigationBar背景的背景.