ios 搜索栏背景色 灰色 ios7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19169715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:01:34  来源:igfitidea点击:

Search Bar background color Gray ios7

iosobjective-cios7uisearchbar

提问by Freddy

I am currently working on an app that worked fine until ios7 came along. The search bar used to be transparent and blended into the blue background of the navigation bar. Now that I am working in ios7, the nav bar is blue, however the search bar has a gray background to it. How do I make it blue or transparent?

我目前正在开发一个运行良好的应用程序,直到 ios7 出现。搜索栏曾经是透明的,并融入了导航栏的蓝色背景中。现在我在 ios7 中工作,导航栏是蓝色的,但是搜索栏的背景是灰色的。我如何使它变成蓝色或透明?

Here is an image:

这是一张图片:

enter image description here

在此处输入图片说明

回答by Quang Hà

Try this:

尝试这个:

if(IOS_7)
{
    self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
}

回答by David Douglas

You can set "Bar Tint" to "Clear Color" in Interface Builder (.xib):

您可以在 Interface Builder (.xib) 中将“Bar Tint”设置为“Clear Color”:

enter image description here

在此处输入图片说明

It can also be done in code:

也可以在代码中完成:

self.searchBar.barTintColor = [UIColor clearColor];

回答by Gabriel Cartier

To make it a flat color, you simply need to remove the UISearchBarBackground view.

要使其成为平面颜色,您只需删除 UISearchBarBackground 视图。

I created a recursive method to properly clean the search bar.

我创建了一个递归方法来正确清理搜索栏。

- (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view
{
    for (UIView *subview in [view subviews]) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [subview removeFromSuperview];
            break; //To avoid an extra loop as there is only one UISearchBarBackground
        } else {
            [self removeUISearchBarBackgroundInViewHierarchy:subview];
        }
    }
}

You can simply send your search bar to the method and change the color afterward.

您可以简单地将搜索栏发送到该方法,然后更改颜色。

[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.backgroundColor = yourUIColor;

回答by Get Schwifty

Swift 4.2

斯威夫特 4.2

You can use this extension to change Font and Background color of the SearchBar.

您可以使用此扩展程序更改 SearchBar 的字体和背景颜色。

extension UISearchBar {

    var textField: UITextField? {
        let subViews = subviews.flatMap { 
// Set the color to whatever blue color that is in your screenshot
self.searchBar.backgroundImage = [UIImage imageWithColor:[UIColor redColor] cornerRadius:5.0f];
.subviews } guard let tf = (subViews.filter {
// Put this in your app delegate's didFinishLaunchingWithOptions method
// Whatever color you want for searchBarColor
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { // For iOS 7
    UIColor *searchBarColor = [UIColor blueColor];
    [[UISearchBar appearance] setBackgroundColor:searchBarColor];
}
is UITextField }).first as? UITextField else { return nil } return tf } func setTextColor(color: UIColor) { textField?.textColor = color } func setBackgroundColor(color: UIColor) { textField?.backgroundColor = color } }

回答by Matt Tang

**Edit - This worked for me in iOS 7

**编辑 - 这在 iOS 7 中对我有用

// Set it in your viewDidLoad method of your controller
// Replace the yourSearchBar property with whatever you're doing to instantiate the search bar
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { // For iOS 7
{
    UIColor *searchBarColor = [UIColor blueColor];
    self.yourSearchBar.backgroundColor = searchBarColor;
}

If you want all of your search bar's to be a certain color do this:

如果您希望所有搜索栏都是某种颜色,请执行以下操作:

##代码##

If you just want that particular search bar background to be a color:

如果您只想将该特定搜索栏背景设为一种颜色:

##代码##