ios 使 UISearchBar 背景清晰

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20993092/
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-30 23:02:17  来源:igfitidea点击:

Make UISearchBar background clear

iosobjective-cuisearchbar

提问by Vikram

I am not able to clear search bar I have tried to make it clear by setting its background color clear and I have also placed one image under searchbar

我无法清除搜索栏我试图通过将其背景颜色设置为清晰来使其清晰,并且我还在搜索栏下放置了一张图片

I have also made clear background of searchbar

我也明确了搜索栏的背景

 for (UIView *subview in self.searchBarHome.subviews) {
      if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
         [subview removeFromSuperview];//please help me to make clear background of uisearchbar
        break; 
    }
}
[self.searchBarHome setBackgroundColor:[UIColor clearColor]];

回答by staticVoidMan

For iOS7+, all you need to do is:

对于 iOS7+,您需要做的就是:

[self.searchBarHome setBackgroundColor:[UIColor clearColor]];
[self.searchBarHome setBarTintColor:[UIColor clearColor]]; //this is what you want

NOTE: This will not work for iOS6

注意:这不适用于 iOS6



For iOS6+, the following will take care of it even in iOS7:

对于 iOS6+,即使在 iOS7 中,以下内容也会处理它:

[self.searchBarHome setBackgroundColor:[UIColor clearColor]];
[self.searchBarHome setBackgroundImage:[UIImage new]];
[self.searchBarHome setTranslucent:YES];

回答by Eden

Here's my solution in Swift 3 (a combo of Scarafone's and Andrew2M's answers):

这是我在 Swift 3 中的解决方案(Scarafone 和 Andrew2M 的答案的组合):

for view in searchBar.subviews.last!.subviews {
        if type(of: view) == NSClassFromString("UISearchBarBackground"){
            view.alpha = 0.0
        }
}

or alternatively:

或者:

searchBar.barTintColor = UIColor.clear
searchBar.backgroundColor = UIColor.clear
searchBar.isTranslucent = true
searchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)

回答by Ruozi

UISearchBar includes two subviews,they are 'UISearchBarBackground' and 'UITextField'. In IOS8, you need to remove 'UISearchBarBackground' to clear it.

UISearchBar 包括两个子视图,它们是“UISearchBarBackground”和“UITextField”。在 IOS8 中,您需要删除 'UISearchBarBackground' 来清除它。

for (UIView *subview in [[yourSearchBar.subviews lastObject] subviews]) {        
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [subview removeFromSuperview];
        break;
    }
}

回答by Andrew2M

Removing the UISearchBarBackground may result in unknown behavior particularly if as you begin to use the element in more advanced ways.

删除 UISearchBarBackground 可能会导致未知行为,尤其是当您开始以更高级的方式使用该元素时。

Ruozi's answer was my preferred solution for grabbing the UISearchBarBackground element however I would suggest setting the alpha to 0 rather than removing the view with the following:

Ruozi 的回答是我抓取 UISearchBarBackground 元素的首选解决方案,但是我建议将 alpha 设置为 0 而不是使用以下内容删除视图:

for (UIView *subview in [[self.searchBar.subviews lastObject] subviews]) {
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [subview setAlpha:0.0];
        break;
    }
}

Add the above code to your UIViewController subclass that contains an IBOutlet *searchBar. This code snippet will acheive a transparent background for both iOS8 and iOS9.

将上述代码添加到包含 IBOutlet *searchBar 的 UIViewController 子类。此代码片段将为 iOS8 和 iOS9 实现透明背景。

In addition it may be better design decision to include this code in your UISearchBar subclass in order to avoid cluttering your viewController.

此外,将这段代码包含在您的 UISearchBar 子类中可能是更好的设计决策,以避免混淆您的 viewController。

回答by Rog

For anyone trying to find a non hacky solution to this, just set the background image to nil on your Storyboard/Xib file. Literally, simply write nilin the background image field of the UISearchBar.

对于任何试图为此找到非黑客解决方案的人,只需在 Storyboard/Xib 文件中将背景图像设置为 nil。从字面上看,只需nil在 UISearchBar 的背景图像字段中写入即可。

回答by Derek Soike

If you want to change this...

如果你想改变这个...

enter image description here

enter image description here

To this...

对此...

enter image description here

enter image description here

Use:

用:

self.searchBar.searchTextField.backgroundColor = .clear

回答by Romulo BM

Simply use searchBar.searchBarStyle = .minimalworks for me.

简单地使用searchBar.searchBarStyle = .minimal对我有用。

I had used many workarounds for a long time, until i discover this property accidentally, and works like a charm.

我已经使用了很长一段时间的许多变通方法,直到我意外地发现了这个属性,并且效果很好。

回答by ivoroto

My solution working on Swift 4.1 | iOS 11.4where I change the background and text colors of UISearchBar

我的解决方案适用于Swift 4.1 | iOS 11.4,我在其中更改 UISearchBar 的背景和文本颜色

enter image description here

enter image description here

The key is to find the UISearchBar's UITextField component and to change its parameters:

关键是找到 UISearchBar 的 UITextField 组件并更改其参数:

let searchTextField = searchBar.value(forKey: "searchField") as? UITextField
searchTextField?.textColor = UIColor.white
searchTextField?.backgroundColor = UIColor.red
searchTextField?.attributedPlaceholder =  NSAttributedString(string: "Your Placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white.withAlphaComponent(0.5)])

Then I set the "search" and the "clear" image

然后我设置“搜索”和“清晰”图像

//search image
searchBar.setImage(UIImage(named: "icon_search"), for: UISearchBarIcon.search, state: .normal)
//clear image
searchBar.setImage(UIImage(named: "icon_search_clear"), for: UISearchBarIcon.clear, state: .normal)

回答by Scarafone

Setting the backgroundColor and barTintColor did not work me iOS 9+, left me with a black background. However Ruozi solution will work. Here is a swift version of the same code.

设置 backgroundColor 和 barTintColor 对我的 iOS 9+ 不起作用,给我留下了黑色背景。然而,若子解决方案将起作用。这是相同代码的快速版本。

for view in _searchBar.subviews.last!.subviews {
    if view.isKindOfClass(NSClassFromString("UISearchBarBackground")!) {
        view.removeFromSuperview()
    }
}

回答by gmogames

For anyone coming here and not being able to make the remove or alpha solution work for you, make sure you have your searchBar SearchStyle NOTon MINIMALIST. Change it to Default and use any of the codes provided here

对于任何人来到这里,不能够进行删除或您阿尔法解决方案的工作,确保你有你的搜索栏SearchStyle上简约。将其更改为默认值并使用此处提供的任何代码