user7987142
user7987142

Reputation:

iOS 13 UISearchBar appearance and behaviour

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?

Upvotes: 23

Views: 9405

Answers (5)

fs_tigre
fs_tigre

Reputation: 10748

iOS 13+.

searchController.searchBar.searchTextField.backgroundColor = UIColor.white

Upvotes: -1

RyuX51
RyuX51

Reputation: 2887

For setting in globally like in AppDelegate:

if #available(iOS 13, *) {
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .anyColor
}

Upvotes: 6

Avendi Sianipar
Avendi Sianipar

Reputation: 261

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

Upvotes: 22

Tai Le
Tai Le

Reputation: 9296

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

Upvotes: 10

user7987142
user7987142

Reputation:

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.

Upvotes: 4

Related Questions