UISearchController/UISearchBar behaves differently under macOS than iOS
Asked Answered
N

0

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?

Numbskull answered 19/10, 2019 at 21:40 Comment(2)
A bug report has been sent to Apple demonstrating this problem. – Numbskull
Hello, @maddy, any news regarding this issue? – Irretentive

© 2022 - 2024 β€” McMap. All rights reserved.