有没有办法在编辑时只激活搜索栏上的取消按钮? [英] Is there a way to only activate the cancel button on a search bar while editing?
问题描述
我知道您可以使用像这样的文本字段上的清除按钮来执行此操作:
I know you can do this with the clear button on a text field like this:
searchController?.searchBar.searchTextField.clearButtonMode = .whileEditing
但是搜索栏上的取消按钮是否有类似的东西?
But is there something similar for the cancel button on a search bar?
searchController?.searchBar.showsCancelButton = true // this exists
searchController?.searchBar.cancelButtonMode = .whileEditing // I could not find this
谢谢!
推荐答案
如果您不希望搜索栏在用户实际点击搜索控制器的搜索栏之前出现,请不要为取消按钮.默认行为是在用户激活搜索之前不显示取消按钮.
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.
从 iOS 13 开始,您可以使用 UISearchController
的 automaticallyShowsCancelButton
属性,但在 iOS 中它默认为 true
.在 Mac Catalyst 下似乎默认为 false
.
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.
这篇关于有没有办法在编辑时只激活搜索栏上的取消按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!