Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UISearchBar颜色与UINavigationBar不匹配_Ios_Objective C_Uinavigationbar_Uisearchbar_Uicolor - Fatal编程技术网

Ios UISearchBar颜色与UINavigationBar不匹配

Ios UISearchBar颜色与UINavigationBar不匹配,ios,objective-c,uinavigationbar,uisearchbar,uicolor,Ios,Objective C,Uinavigationbar,Uisearchbar,Uicolor,我正在将UISearchBar的barColor设置为与导航栏设置相同的颜色。当我在UI中返回结果时,颜色不匹配。我已将搜索栏和导航栏的“半透明”设置为“是” 有人能告诉我如何使搜索栏的颜色与导航栏的颜色匹配吗?我发现实现这一点最简单的方法是使用图像,而不仅仅是颜色 [mySearchBar setBackgroundImage:[UIImage imageWithColor:[UIColor blackColor] forBarPosition:UIBarPositionAny barMe

我正在将UISearchBar的
barColor
设置为与导航栏设置相同的颜色。当我在UI中返回结果时,颜色不匹配。我已将搜索栏和导航栏的“半透明”设置为“是”


有人能告诉我如何使搜索栏的颜色与导航栏的颜色匹配吗?

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

[mySearchBar setBackgroundImage:[UIImage imageWithColor:[UIColor blackColor] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
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;
}

这样,我就能够使
UISearchBar
背景与
UINavigationBar
背景完全匹配。

您只需将
UISearchBar
backgroundColor
设置为
UIColor.clearColor()
。然后,
BartinColor
将如您所料通过。

我回答了另一个与此非常相似的SO问题。只需将
半透明
设置为
不会起作用。为
UINavigationBar
设置时,您必须稍微更改用于
UISearchBar
的颜色