I have a search bar set up using the .searchable modifier on a SwiftUI View, and I would like to set the background color of the bar text field to be Color.black.
I’ve attempted to change the background color in the init() of the app’s @main entry point. It does indeed change the background color to black, but its somewhat of a faded black, a bit lighter, its not the perfect dark black of Color.black that you would normally see when setting it in a View.
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [.foregroundColor: UIColor.white]
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor(.black)
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal) // Cancel button
UISearchBar.appearance().overrideUserInterfaceStyle = .dark
Cropped image of top navigation bar showcasing my issue
Matias Carulli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.