简体   繁体   中英

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 外观 vs iOS 13: 在此处输入图片说明 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?

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:

固定前 固定后

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

For setting in globally like in AppDelegate :

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

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.

iOS 13+。

searchController.searchBar.searchTextField.backgroundColor = UIColor.white

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM