D
D
Darkness2020-02-24 13:49:16
iOS
Darkness, 2020-02-24 13:49:16

Why can't cancelButton in searchBar have time to be renamed?

Problem:
Trying to rename the Cancel button for a UISearchBar, it gets renamed, but not on the first interaction. What's my mistake?

Details:
I'm trying to implement in UISearchBarDelegate, namely in searchBarShouldBeginEditing
Here's how it looks:

let customCancelButton = searchBar.value(forKey: "cancelButton") as? UIButton
    customCancelButton?.setTitle("Отменить", for: .normal)
    searchBar.setShowsCancelButton(true, animated: true)
    searchBar.showsCancelButton = true    
    return true


As a result, it is renamed, but not the first time, I'm a beginner, so most likely I just don't see an obvious error! Maybe something is connected with the streams?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Darkness, 2020-02-24
@AntonBrock

Everything turned out to be so simple ...
In order to change the name on the button and not touch the streams duringViewDidLoad()

searchBar.setValue("Отменить", forKey: "cancelButtonText")

cancelButtonText- responsible for this button.
And then, in the delegate method, it simply shows the button itself, as I did:
searchBar.setShowsCancelButton(true, animated: true)
searchBar.showsCancelButton = true

Regarding the problem:
I put the name change that was in the question itself into another thread
DispatchQueue.main.async{ // changed the name here}, and already below I did the button display - the problem was almost solved, but the UI did not see the size of the button the first time.
If someone can explain this moment - there will be an answer to the question. Because that's the question, not about how to rename the button! I would really appreciate an explanation!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question