UISearchBar取消按钮不可用的解决方法

    xiaoxiao2022-07-02  128

    UISearchBar失去焦点时取消按钮会不可用,我们可以在UISearchBar失去焦点时(例如点击搜索,拖动滚动条等),将取消按钮isEnabled设置为true,使用KVC不会导致App Store审核被拒。

    extension UISearchBar{ func setCancelButtonEnable(){ if let cancelButton = value(forKey: "cancelButton") as? UIButton{ cancelButton.isEnabled = true } } } func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { searchBar.resignFirstResponder() searchBar.setCancelButtonEnable() } func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { searchBar.resignFirstResponder() searchBar.setCancelButtonEnable() }
    最新回复(0)