ios 如何在输入文本时保持显示书签按钮?

xa9qqrwz  于 2023-06-07  发布在  iOS
关注(0)|答案(1)|浏览(224)

我目前正在Swift中使用UISearchController
我想在文本输入到textField时继续显示书签按钮。
我需要通过一个书签按钮(或任何其他可能存在的方式),而不是通过“searchBarSearchButtonClicked”方法时,搜索被调用的文本。
但是,一旦输入文本,该按钮就会隐藏,因为在其上方会显示一个清除按钮。
我将下面的选项设置为“.never”,不幸的是,它没有显示书签按钮,尽管一个清除按钮消失了。

searchController.searchBar.searchTextField.clearButtonMode = .never

问题:

有没有办法解决这个问题,让它发挥作用?
我想这是UISearchController的规格。
如果没有的话,如果你能告诉我其他的方法,我将不胜感激。
下面是我的代码和屏幕图像,供您参考。
| 在输入文本之前|输入文本后|
| - -----|- -----|
|

|

|

属性:

let searchController = UISearchController(searchResultsController: nil)

setupSearchController:

我在下面的方法中设置了一个书签按钮和清除按钮。

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"

    // bookmark and clear button settings
    searchController.searchBar.searchTextField.clearButtonMode = .never
    searchController.searchBar.showsBookmarkButton = true
    searchController.searchBar.setImage(UIImage(systemName: "globe"), for: .bookmark, state: .normal)
}

**searchBarSearchButtonClicked:**搜索过程通过下面的方法执行。

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    print("searchBarSearchButtonClicked")
    print("keyword", searchBar.text ?? "Empty keyword")
    guard let keyword = searchBar.text else {
        print("Keyword is empty")
        return
    }
    // Search process is here...
}

**searchBarBookmarkButtonClicked:**我想使用下面方法中输入的文本执行另一个进程(不是搜索进程)。

func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
    print("Bookmark button is clicked...")
    guard let searchQuery = searchBar.text, !searchQuery.isEmpty else {
        print("Keyword is empty")
        return
    }
    // Another process is here...
}

如果你需要更多的信息,请告诉我。

yeotifhr

yeotifhr1#

找到了书签未显示问题的解决方案:

searchController.searchBar.showsBookmarkButton = true

根据搜索栏是否处于活动状态,确保在代码中或updateSearchResults函数中的任何地方都不会将此设置为false。

相关问题