I was trying to make the textField in searchBar the first responder, but it always failed.
What I have done:
searchBar.textField.canBecomeFirstResponder returns truesearchBar.textField.becomeFirstResponder() returns falsefunc findFirstResponder() -> Any?
{
if (self.isFirstResponder) {
return self
}
for subView in self.view.subviews {
if subView.isFirstResponder {
return subView
}
}
return nil
}
I don't know whether I was missing something or It seems to be a bug.
Any help would be appreciated.
When are you calling this function? Can you send me a sample with this and I will take a look?
@danieldahan
I use the Search Sample from https://github.com/CosmicMind/Samples
I try to add searchBar.textField.becomeFirstResponder() in two places.
prepareSearchBar() in AppSearchBarController.swiftprepareSearchBar() in RootViewController.swiftThanks for you time ;)
Yeah, I can see it is not working. Weird, well I shall take a look :)
Hey, so you need to call becomeFirstResponder() when the view controller has appeared, like so:
open override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
searchBar.textField.becomeFirstResponder()
}
:) All the best!
That works! Thank you! ;)
I should have investigated the view life cycles better.
No worries :) I am happy to help.
Most helpful comment
Hey, so you need to call
becomeFirstResponder()when the view controller has appeared, like so::) All the best!