【问题标题】:ios searchbar textfield background not correct as setup colorios搜索栏文本字段背景与设置颜色不正确
【发布时间】:2019-10-24 07:16:42
【问题描述】:

当我更改 UISearchBar 中文本字段的背景颜色时,我发现颜色设置不正确。例如:我设置了颜色#2B2F6A,但它变成了#343869

对此有什么建议吗?非常感谢。

Code:
iOS < 13

if let textField = self.value(forKey: "searchField") as? UITextField {
    if let backgroundView = textField.subviews.first {
      backgroundView.backgroundColor = UIColor(red: 28.0/255.0, green: 28.0/255.0, blue: 28.0/255.0, alpha: 1.0)
    }
}

iOS 13:

self.searchTextField.backgroundColor = UIColor(red: 28.0/255.0, green: 28.0/255.0, blue: 28.0/255.0, alpha: 1.0)

设置的颜色可能是灰色,但结果看起来像黑色

【问题讨论】:

  • 你在哪个 iOS 版本上检查这个?
  • 设置颜色后能否提供一些示例代码?

标签: ios searchbar


【解决方案1】:

在 iOS13 上,即使 UISearchTextField 类型的 UISearchBar 属性 searchTextField 是 UITextField 子类,并且在the related session of WWDC 中声称开发人员可以像使用 UITextField 一样自定义 textField 属性,但有些属性不能继续尊重这份“合同”。例如更改 rightView 是不可定制的。想必背景颜色也是如此。

更新:我进行了检查,我可以在 iOS13 上毫无问题地设置 backgroundColor - 你使用的是 searchTextField 属性吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 2013-04-02
    • 2013-10-10
    • 2020-01-26
    • 2014-05-20
    相关资源
    最近更新 更多