SwiftUI 更改搜索导航栏背景颜色

问题描述 投票:0回答:1

如何更改导航栏搜索字段的背景颜色?

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])

我也不知道如何更改图标颜色。

感谢您的帮助。

search swiftui uitextfield uinavigationbar
1个回答
0
投票

嘿,你有没有想过这个?谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.