自定义“可搜索”搜索字段 SwiftUI iOS 15

Jon*_*gel 8 search uisearchbar swift swiftui

在 iOS 15 上的 SwiftUI 中使用新.searchable()修饰符时,我无法自定义搜索栏外观。具体来说,我不希望它与我用于导航栏的颜色看起来很好。在此输入图像描述

我尝试.appearance() 像这样改变。

UISearchBar.appearance().backgroundColor = UIColor.white
UISearchBar.appearance().tintColor = UIColor.white
UISearchBar.appearance().barTintColor = UIColor.white
Run Code Online (Sandbox Code Playgroud)

但只设法得到了这个。

在此输入图像描述

尽管这种方式很成功,但间距看起来并不好。我宁愿把它染成白色。

Jon*_*gel 10

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

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .white
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 对我来说,tintColor 似乎没有效果。您是否尝试过将颜色设置为黑色以外的任何颜色? (2认同)