Md Tariqul Islam
Md Tariqul Islam

Reputation: 2804

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")
}

Upvotes: 5

Views: 1795

Answers (1)

someone
someone

Reputation: 194

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.

Upvotes: 4

Related Questions