【UISearchController / UISearchBar】如何给搜索图标添加点击手势?

【UISearchController / UISearchBar】How to add tap gesture to search icon?

我目前 using UISearchController 在我的应用程序中。

我想在点击 leftView 的搜索图标时使用在 textField 中输入的文本执行一个过程(不是搜索过程)。

因此,我尝试为图标添加点击手势(或长按手势)using UIGestureRecognizer and UILongPressGestureRecognizer,但没有任何反应。

问题一:
Does the icon originally not trigger any actions when tapped? (It doesn't even call "searchBarSearchButtonClicked" function when tapped.)

问题二:
Is there any way to use the icon like clickable button, and trigger a specific action when tapped?

供您参考,我的代码和屏幕图像如下。
如果您需要其他信息,请告诉我。
提前谢谢你。

更新:书签按钮没有按预期工作...
我尝试了书签按钮。但是,它没有按预期工作,因为它在输入文本时隐藏在一个清晰的图标下面。
如果在输入文本后书签按钮一直显示就更好了。

目标图标图片

属性:

let searchController = UISearchController(searchResultsController: nil)
var keyword = ""

设置搜索控制器:
我在下面的函数中更改了搜索图标的颜色和图像。

private func setupSearchController() {
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = true
    definesPresentationContext = true

    searchController.delegate = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchResultsUpdater = self

    searchController.searchBar.delegate = self
    searchController.searchBar.placeholder = "Search channels"

    // search icon settings
    searchController.searchBar.searchTextField.leftView?.tintColor = .link
    searchController.searchBar.setImage(UIImage(systemName: "globe"), for: .search, state: .normal)
}

设置翻译按钮:
我尝试了两种方法,UITapGestureRecognizerUILongPressGestureRecognizer,这两种方法都没有发生任何事情。

// UITapGestureRecognizer version
private func setupTranslateButton() {
    let transButton = searchController.searchBar.searchTextField.leftView!

    let tap = UITapGestureRecognizer(target: self, action: #selector(translateKeyword))
        transButton.addGestureRecognizer(tap)
    searchController.searchBar.searchTextField.addSubview(transButton)
}

// UILongPressGestureRecognizer version
private func setupTranslateButton() {
    let transButton = searchController.searchBar.searchTextField.leftView!

    let tap = UILongPressGestureRecognizer(target: self, action: #selector(translateKeyword))
    tap.minimumPressDuration = 0
    transButton.addGestureRecognizer(tap)
} 

翻译关键词:
我想在成功触发点击手势时调用以下函数。

@objc func translateKeyword(_ sender: UILongPressGestureRecognizer) {

    print("Translate button is clicked...")

    // keyword string should exist by the process executed in "updateSearchResults" function 
    if keyword.isEmpty {
        print("Keyword is empty")
        return
    }

    // The following process is here...
}

updateSearchResults:
每次在 textField 中输入时,所有文本都会放入关键字变量中。

func updateSearchResults(for searchController: UISearchController) {
        
    print("updateSearchResults")
       
    guard let inputKeyword = searchController.searchBar.text, !inputKeyword.isEmpty else {
        print("Keyword is not entered...")
        return
    }
        
    keyword = inputKeyword
}

因此,为了解决您的挑战,我发现有两件事我认为您需要做,因为我已经测试过并且它在这里有效。

1.

 private func setupTranslateButton() {
        let transButton = searchController.searchBar.searchTextField.leftView!

        let tap = UITapGestureRecognizer(target: self, action: #selector(translateKeyword))
        //Add this line of user interaction Enabled to be true;
        transButton.isUserInteractionEnabled = true;
        searchController.searchBar.setImage(UIImage(systemName: "globe"), for: .search, state: .normal)
            transButton.addGestureRecognizer(tap)
        searchController.searchBar.searchTextField.addSubview(transButton)
    }
  1. 在下面的函数中调用

        private func setupSearchController() {
         navigationItem.searchController = searchController
         navigationItem.hidesSearchBarWhenScrolling = true
         definesPresentationContext = true
         searchController.delegate = self
         searchController.obscuresBackgroundDuringPresentation = false
         searchController.searchResultsUpdater = self
    
         searchController.searchBar.delegate = self
         searchController.searchBar.placeholder = "Search channels"
    
         // search icon settings
         searchController.searchBar.searchTextField.leftView?.tintColor = .link
         // calling it here
         setupTranslateButton()
     }
    
  2. 在viewDidLoad下面调用这个项目。

    setupSearchController()

请注意,这是假设您已经实现了 SearchbarController class 的所有必要协议和委托。这解决了您的挑战。