即使showCancelButton等于false或将IOS 13标记为自动取消按钮解除功能,按键盘上的搜索按钮然后按取消按钮也不会总是取消该取消按钮。这在模拟器中有效,但在我的设备上只有50%的时间。有人知道任何可能的解决方案吗?
func setupSearchController() {
if #available(iOS 13.0, *) {
searchController.automaticallyShowsCancelButton = true
} else {
searchController.searchBar.showCancelButton = false
}
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
if #available(iOS 13.0, *) {
searchController.automaticallyShowsCancelButton = true
} else {
searchController.searchBar.showCancelButton = false
}
}
func updateSearchResults(for searchController: UISearchController) {
if #available(iOS 13.0, *) {
searchController.automaticallyShowsCancelButton = true
} else {
searchBar.showsCancelButton = true
}
}
答案 0 :(得分:0)
如果我正确理解了问题所在,那似乎是时机不好的问题。在这种情况下,解决方案是短暂延迟:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
{
if #available(iOS 13.0, *)
{
let deadlineTime = DispatchTime.now() + .seconds(0.3)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
// your code here, such as
searchController.automaticallyShowsCancelButton = true
}
}
else
{
searchController.searchBar.showCancelButton = false
}
}