Customize "searchable" Search field SwiftUI iOS 15

Viewed 1581

When using the new .searchable() modifier in SwiftUI on iOS 15 I have no way to customize the Search Bar appearance. Specifically, I wan't it to look good with the color I'm using for my Navigation Bar.enter image description here

I tried altering the .appearance() like this.

UISearchBar.appearance().backgroundColor = UIColor.white
UISearchBar.appearance().tintColor = UIColor.white
UISearchBar.appearance().barTintColor = UIColor.white

But only managed to get this.

enter image description here

Even though this kind of succeeded the spacing does not look good. I would rather tint it white.

2 Answers

I solved this by using these two global appearance lines.

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .white
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black

enter image description here

The textColor of the input field can be changed from black to white by altering the .preferredColorScheme() of the view to which the .searchable modifier is applied. Setting .preferredColorScheme(.dark) will set the color to white.

Related