UISearchBar barColor与UINavigationBar不匹配

时间:2014-07-09 21:26:00

标签: ios objective-c uinavigationbar uisearchbar uicolor

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

enter image description here enter image description here

有谁能告诉我如何才能获得搜索栏的颜色以匹配导航栏的颜色?

3 个答案:

答案 0 :(得分:6)

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

[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背景完全匹配。

答案 1 :(得分:0)

You can just set the <a href="http://lorempixel.com/400/400/sports/1" data-lightbox="image-1" data-title="Sports 1"> <img src="http://lorempixel.com/200/200/sports/1" alt=""/> </a> of the backgroundColor to UISearchBar. Then the UIColor.clearColor() will come through as you expect.

答案 2 :(得分:0)

我回答了另一个与此问题非常相似的问题。只需将translucent设置为NO即可。当您为UISearchBar设置UINavigationBar时,您必须略微改变用于<header> <div id="header-top"> <div class="container"> </div><div class="header-image"><img class="img-responsive" src="http://zgraggen-zahnarzt.ch/templates/adventurestrap-light/assets/carousel-images/001.jpg"></div> <nav class="navbar navbar-default"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false"> <span class="sr-only">"Toggle-navigation"</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> </div> <div class="container"> <div id="navbar" class="navbar-collapse collapse"> <ul class="nav navbar-nav navbar-right"> <li class=" dropmenu color-random "> <a href="http://zgraggen-zahnarzt.ch">Start <i class="fa fa-caret-down"></i></a> <ul class="dropdown-menu"> <li class=" "> <a href="http://zgraggen-zahnarzt.ch/pages/start/test.php">Test</a> </li> </ul> </li> <li class="active color-random "> <a href="http://zgraggen-zahnarzt.ch/pages/kontakt.php">Kontakt</a> </li> <li class=" color-random "> <a href="http://zgraggen-zahnarzt.ch/pages/oeffnungszeiten.php">Öffnungszeiten</a> </li> <li class=" color-random "> <a href="http://zgraggen-zahnarzt.ch/pages/anfahrt.php">Anfahrt</a> </li> </ul> </div><!-- /.container --></div></div> </nav> 的颜色。 Check out my full answer here