【问题标题】:Customize "searchable" Search field SwiftUI iOS 15自定义“可搜索”搜索字段 SwiftUI iOS 15
【发布时间】:2021-10-10 02:57:12
【问题描述】:

在 iOS 15 的 SwiftUI 中使用新的 .searchable() 修饰符时,我无法自定义搜索栏的外观。具体来说,我不希望它与我用于导航栏的颜色相得益彰。

我尝试像这样更改.appearance()

UISearchBar.appearance().backgroundColor = UIColor.white
UISearchBar.appearance().tintColor = UIColor.white
UISearchBar.appearance().barTintColor = UIColor.white

但只能设法得到这个。

即使这种成功,间距也不好看。我宁愿把它染成白色。

【问题讨论】:

  • 你能提供一些额外的代码吗?例如,ContentView 中的代码?这是因为通过添加 searchable() 修饰符,TextField 是透明的
  • 您可以实现 init() 函数来初始化这些属性。当我使用 UINavigationBar.appearnce() 更改 tintColor 时,它实际上对我有用。但我仍在学习如何更改 textColor。

标签: swift search swiftui uisearchbar


【解决方案1】:

我通过使用这两条全局外观线解决了这个问题。

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .white
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black

【讨论】:

  • 对我来说,tintColor 似乎没有效果。您是否尝试将颜色设置为黑色以外的任何颜色?
猜你喜欢
  • 2021-12-07
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
  • 1970-01-01
  • 2019-12-16
相关资源
最近更新 更多