无法在 iOS 8 中将搜索栏色调颜色更改为透明

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

Can't change search bar tint color to be transparent in iOS 8

iosiphoneios8uisearchbarsearchbar

提问by ox_

Upgraded from Xcode 5 to 6 and now my search bar tint is black.

从 Xcode 5 升级到 6,现在我的搜索栏色调是黑色的。

Tried to change it through storyboard right pane > "Bar Tint" to clear color, but it's still black.

试图通过故事板右窗格>“Bar Tint”更改它以清除颜色,但它仍然是黑色。

Also tried programmatically:

还以编程方式尝试过:

[self.searchBar setTintColor:[UIColor clearColor]];

Still black :(

还是黑的:(

Any ideas?

有任何想法吗?

回答by Mike

The tintColorproperty on search bars, much like UINavigationBar, changes the color of the buttons, as well as changes the color of the blinking cursor, not the actual search bar background. What you want to use is the barTintColorproperty.

tintColor搜索栏上的属性,很像 UINavigationBar,改变按钮的颜色,以及改变闪烁光标的颜色,而不是实际的搜索栏背景。您要使用的是barTintColor属性。

searchbar.barTintColor = [UIColor orangeColor];
searchbar.tintColor = [UIColor greenColor];

Produces the following ugly, yet informative, result:

产生以下丑陋但信息丰富的结果:

enter image description here

在此处输入图片说明

If you want to have a completely transparent search bar, you need to set the background image as well:

如果你想要一个完全透明的搜索栏,你还需要设置背景图片:

searchbar.barTintColor = [UIColor clearColor];
searchbar.backgroundImage = [UIImage new];

enter image description here

在此处输入图片说明

EDIT:I would strongly advise against traversing and modifying the subviews of anyUIKit object, as has been proposed in other answers. From Apple's documentation:

编辑:我强烈建议不要像其他答案中提出的那样遍历和修改任何UIKit 对象的子视图。来自苹果的文档:

For complex views declared in UIKit and other system frameworks, any subviews of the view are generally considered private and subject to change at any time. Therefore, you should not attempt to retrieve or modify subviews for these types of system-supplied views. If you do, your code may break during a future system update.

对于 UIKit 和其他系统框架中声明的复杂视图,视图的任何子视图通常都被视为私有的,并且随时可能更改。因此,您不应尝试检索或修改这些类型的系统提供的视图的子视图。如果这样做,您的代码可能会在未来的系统更新期间中断。

https://developer.apple.com/documentation/uikit/uiview/1622614-subviews

https://developer.apple.com/documentation/uikit/uiview/1622614-subviews

回答by darqueos

I got to change it on iOS 9 using Swift 2.0 this way:

我必须以这种方式使用 Swift 2.0 在 iOS 9 上更改它:

    let image = UIImage()

    searchBar.setBackgroundImage(image, forBarPosition: .Any, barMetrics: .Default)
    searchBar.scopeBarBackgroundImage = image

回答by Gilad Brunfman

On Swift 3:

在 Swift 3 上:

Setting up the background of the searchBar to an empty image:

将 searchBar 的背景设置为空图像:

    searchBar.setBackgroundImage(image, for: .any, barMetrics: .default)
    searchBar.scopeBarBackgroundImage = image

回答by krishnan

for programmatically change search bar tint color :

以编程方式更改搜索栏色调颜色:

    if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
        textfield.textColor = #colorLiteral(red: 0.3921568627, green: 0.3921568627, blue: 0.3921568627, alpha: 1)
        textfield.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
    }

for storyboard :

对于故事板:

enter image description here

在此处输入图片说明

回答by krishnan

I've been seeing this problem as well on iOS8 / XCode 6. I did not get the search bar translucent. searchbar.barTintColor or searchbar.tintColor setting to clear color did not help or shows a black searchbar.

我在 iOS8/XCode 6 上也看到过这个问题。我没有让搜索栏变成半透明。searchbar.barTintColor 或 searchbar.tintColor 设置清除颜色没有帮助或显示黑色搜索栏。

The only workaround i found was to set a translucent background png image (alpha=0.0) and setting searchbar.barTintColor to clear color.

我发现的唯一解决方法是设置半透明背景 png 图像 (alpha=0.0) 并将 searchbar.barTintColor 设置为清除颜色。