【UISearchController / UISearchBar】How to keep showing a bookmark button when text is entered?

Viewed 379

This is the first time I post a question in Stack Overflow.

I'm currently struggling with UISearchController in Swift.
I'd like to keep showing a bookmark button when text is entered in textField.

I need to take the text via a bookmark button(or any other ways if exists) other than via "searchBarSearchButtonClicked" method called when search is executed.

However, the button is hidden once text is entered, as a clear button is shown over it.

I set the following option to ".never", which unfortunately didn't show a bookmark button although a clear button disappeared.

searchController.searchBar.searchTextField.clearButtonMode = .never

Question:
Is there any way to resolve the issue and make it work?

I suppose it's the specifications of UISearchController.
If not, I'd be appreciate if you could tell me some alternative ways.

For your reference, my code and screen images are below.

Before entering text After entering text
image before entering text image after entering text

Properties:

let searchController = UISearchController(searchResultsController: nil)

setupSearchController:
I set up a bookmark button and clear button in the method below.

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: Search process is executed in the method below.

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: I'd like to execute another process(NOT search process) using entered text in the method below.

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

If you have some additional information you need, please let me know.
Thank you in advance.

1 Answers

Found a solution for bookmark not seen problem:

searchController.searchBar.showsBookmarkButton = true

Make sure this is not made false anywhere in the code or in the updateSearchResults function based on whether the searchbar is active or not.

Please refer this article for a proper setup as required.

Related