【问题标题】:Cancel button in UISearchController doesn’t disappear properly in IOS 13UISearchController 中的取消按钮在 iOS 13 中没有正确消失
【发布时间】:2019-10-21 22:24:16
【问题描述】:

按下键盘上的搜索按钮然后按下取消按钮并不总是关闭取消按钮,即使 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
    }
}

【问题讨论】:

标签: swift ios13 uisearchcontroller


【解决方案1】:

如果我正确理解了这个问题,那似乎是时机不对的问题。在这种情况下,解决方案是短暂延迟:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    相关资源
    最近更新 更多