这是我第一次在 Stack Overflow 上发布问题。
我目前正在为 UISearchController
苦苦挣扎在 swift 。
我要 输入文本时保持显示书签按钮 在文本字段中。
我需要通过书签按钮(或任何其他方式,如果存在)而不是通过执行搜索时调用的“searchBarSearchButtonClicked”方法获取文本。
但是,一旦输入文本,按钮就会隐藏,因为它上面会显示一个清除按钮。
我将以下选项设置为“.never”,不幸的是,尽管清除按钮消失了,但它没有显示书签按钮。
searchController.searchBar.searchTextField.clearButtonMode = .never
问题:Is there any way to resolve the issue and make it work?
我想这是 UISearchController 的规范。如果没有,如果您能告诉我一些替代方法,我将不胜感激。
供您引用,我的代码和屏幕图像如下。
输入文字前
输入文字后
属性:
let searchController = UISearchController(searchResultsController: nil)
设置搜索 Controller :我在下面的方法中设置了书签按钮和清除按钮。
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...
}
如果您有需要的其他信息,请告诉我。先感谢您。
最佳答案
找到书签未显示问题的解决方案:
searchController.searchBar.showsBookmarkButton = true
根据搜索栏是否处于事件状态,确保在代码或 updateSearchResults 函数中的任何地方都没有将其设为 false。请引用this article根据需要进行正确设置。
关于ios - 【UISearchController/UISearchBar】如何在输入文字时一直显示书签按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67260112/