UISearchController中的“取消”按钮在IOS 13中无法正常消失

时间:2019-10-21 22:24:16

标签: swift ios13 uisearchcontroller

即使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
    }
}

1 个答案:

答案 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
    }
}
相关问题