如何更改导航栏搜索字段的背景颜色?
init() {
let navBarAppearance = UINavigationBar.appearance()
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.black]
navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])
.attributedPlaceholder = NSAttributedString(string: "Search...", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
}
var body: some View {
NavigationStack {
ZStack {
Image("Background")
.resizable()
.edgesIgnoringSafeArea(.all)
VStack() {
ZStack {
RoundedRectangle(cornerRadius: 20)
.foregroundColor(Color("BackgroundColor"))
VStack {
List(searchResults, id: \.self) { food in
Text(food)
}
.frame(height: 600)
.searchable(text: $searchText)
Spacer()
}
}
.cornerRadius(10)
.frame(height: 80)
VStack {
Text("")
}
}
.padding()
}
.navigationTitle("List Search")
.navigationBarTitleDisplayMode(.inline)
}
}
我能够在 init() 中使用以下代码更改前景色:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) .attributedPlaceholder = NSAttributedString(string: "Search...", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
我也不知道如何更改图标颜色。
感谢您的帮助。
嘿,你有没有想过这个?谢谢!