Search code examples
iosswiftswiftuisearchable

How to change searchable search icon color in swiftui


I want to change the search icon color. is there a way to change it?

NavigationView {
  Text("Searching for \(searchText)")
  .searchable(text: $searchText, prompt: "Look for something")
  .foregroundColor(.red)
  .navigationTitle("Searchable Example")
}

Solution

  • Yes there is a way. Basically you have to create your own magnifying glass.

    Below code you would typically put into a class function and call it when the view appears.

    UISearchBar.appearance().setImage(searchBarImage(), for: .search, state: .normal)
    
    private func searchBarImage() -> UIImage {
        let image = UIImage(systemName: "magnifyingglass")
        return image!.withTintColor(UIColor(.red), renderingMode: .alwaysOriginal)
    }
    

    IF this is not what you're looking for, then look into UISearchBar.appearance() properties, you can change lots of things through it.