23

I have my UISearchBar set up as follows:

searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false // Allow user to tap on results
searchController.searchBar.placeholder = "Search patients" // Placeholder
searchController.searchBar.barStyle = .blackOpaque
searchController.searchBar.tintColor = colors.text // Cancel button tint

navigationItem.searchController = searchController // Set the searchController
navigationItem.hidesSearchBarWhenScrolling = true // Auto-hide search when user scrolls

This is how it looks on iOS 12: iOS 12 appearance vs iOS 13: enter image description here What's changed in iOS 13? I've tried going through the different barStyles, and also setting .isTranslucent to false - no effect for either. Light/dark mode also don't change anything.

The other change is hiding the search bar - on iOS 12 if I scrolled upwards a little the search bar would hide (didn't matter if the table was populated or not). With iOS 13, once the search bar has appeared (ie the user has swiped down), it cannot be hidden again. Anyone know of a fix for this too?

5 Answers5

22

I get something similar problem with you. I don't know why this happen in currently iOS 13 and work properly in older version. But i have found the solution by adding this function to your searchBar.

if #available(iOS 13.0, *) {
   searchBar.searchTextField.backgroundColor = UIColor.white
}

Preview after fixing:

before fixing after fixing

Avendi Sianipar
  • 261
  • 1
  • 9
10

How about to use searchBarStyle as default and change the searchTextField background color?

if #available(iOS 13.0, *) {
    searchBar.searchBarStyle = .default
    searchBar.searchTextField.backgroundColor = UIColor.black.withAlphaComponent(0.1)
}
Tai Le
  • 8,530
  • 5
  • 41
  • 34
6

For setting in globally like in AppDelegate:

if #available(iOS 13, *) {
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .anyColor
}
RyuX51
  • 2,779
  • 3
  • 26
  • 33
  • Any reason that `UISearchBar.appearance().searchTextField.backgroundColor = .anyColor` wouldn't work here? – Sterling C. Apr 18 '20 at 14:58
  • This is what I used to fix my SearchBar input field background: `UITextField.appearance().backgroundColor = .white`. Solved my issue for iOS 15 and 16! – dbDev Jan 05 '23 at 16:06
4

searchController.searchBar.searchTextField.backgroundColor = UIColor.black does the job as a workaround. The selector is new in iOS 13.

I've filed a report on feedback assistant anyway as I do believe this to be unexpected behaviour.

-1

iOS 13+.

searchController.searchBar.searchTextField.backgroundColor = UIColor.white
fs_tigre
  • 10,650
  • 13
  • 73
  • 146