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

标签 ios iphone ios8 uisearchbar searchbar

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

试图通过 Storyboard 右 Pane >“Bar Tint”将其更改为清除颜色,但它仍然是黑色。

也以编程方式尝试:

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

还是黑的:(

有什么想法吗?

最佳答案

搜索栏上的 tintColor 属性与 UINavigationBar 非常相似,可更改按钮的颜色,以及更改闪烁光标的颜色,而不是实际的搜索栏背景。您要使用的是 barTintColor 属性。

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

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

enter image description here

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

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

enter image description here

编辑:我强烈建议不要遍历和修改任何 UIKit 对象的 subview ,正如其他答案中所建议的那样。来自 Apple 的文档:

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.

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

关于ios - 无法将搜索栏色调颜色更改为在 iOS 8 中透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26064677/

相关文章:

ios - iOS 8 上未返回正确的自动布局宽度

ios - 设置为 nil 或 ""时的 UILabel 文本属性会使 UILabel 从 View 中消失 (Swift/Autolayout/iOS9.1)

iphone - 在 Objective C 编程中哪个是更可取的几何?

ios - 如何在 iOS 6 中禁用 UITableView 水平滚动?

ios - NSString 格式问题 - {( String )}

ios - 如何为不同的屏幕尺寸设置不同的字体,例如iPhone 6s和iPhone SE

ios - iPhone在没有私有(private)图书馆的情况下获得SSID

iphone - 我们可以从应用商店获得唯一的交易ID吗

ios - NSUserDefaults 不适用于 IOS 8 模拟器

ios - 在 iOS8 中以编程方式禁用 'Hey Siri'