8

I have a standard UISearchController with a standard UISearchBar. The search bar is shown in the navigation bar. Everything works as expected on iOS/iPadOS. But the search bar behaves differently under macOS.

Under iOS, when you tap on the search bar the icon only appears once there is text in the search bar. It doesn't show when there is no text. If you tap on the icon, the text in the search bar is removed. All good so far.

But under macOS, the icon appears immediately and stays in view whether the search bar has text or not. Worse, when you click on the icon, not only is the text removed from the search bar, the whole search controller is cancelled. It's exactly as if the Cancel button was clicked. In fact, the UISearchBarDelegate method searchBarCancelButtonClicked is called when clicking on the icon (but not in iOS).

For reference, here is the related code in the viewDidLoad method of a simple view controller that demonstrates the issues:

navigationItem.rightBarButtonItem = editButtonItem

let results = UITableViewController(style: .plain)

let sc = UISearchController(searchResultsController: results)
sc.delegate = self
sc.obscuresBackgroundDuringPresentation = true
sc.automaticallyShowsCancelButton = true
sc.hidesNavigationBarDuringPresentation = true // Setting to false causes other problems unrelated to this question

sc.searchBar.delegate = self

navigationItem.searchController = sc

On a real macOS app such as the standard Mail app, the search bar behaves just like UISearchBar does on iOS, not like UISearchBar does on macOS.

Is this a bug in Mac Catalyst or is there some property or delegate I'm missing that will get the UISearchBar and its icon to work under macOS just as it does under iOS?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

0 Answers0