Is there a way to only activate the cancel button on a search bar while editing?

蹲街弑〆低调 提交于 2020-02-05 02:38:07

问题


I know you can do this with the clear button on a text field like this:

self.searchController?.searchBar.searchTextField.clearButtonMode = .whileEditing

But is there something similar for the cancel button on a search bar?

self.searchController?.searchBar.showsCancelButton = true // this exists
self.searchController?.searchBar.cancelButtonMode = .whileEditing // i couldnt find this

Thank you!


回答1:


If you don't want the search bar to appear until the user actually taps on the search bar of the search controller, don't add any code for the cancel button. The default behavior is to not show the cancel button until the user activates search.

Simply remove any code you have to setup or change the cancel button.

As of iOS 13 you can use the automaticallyShowsCancelButton property of UISearchController but in iOS it defaults to true. It seems to default to false under Mac Catalyst.



来源:https://stackoverflow.com/questions/58468949/is-there-a-way-to-only-activate-the-cancel-button-on-a-search-bar-while-editing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!