When calling becomeFirstResponder()
on a UITextField
the cursor animates in from the top left corner. How do I remove it?
The UITextField is in a SearchBar.
[SWIFT 4]
There's another workaround we can make, based on the answer above, that disables this native cursor entrance animation.
Lets observe the keyboard events such as:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: .UIKeyboardDidShow, object: nil)
and then, on each method:
@objc func keyboardWillShow(_ notification: Notification) {
searchBar.tintColor = UIColor.clear
}
@objc func keyboardDidShow(_ notification: Notification) {
searchBar.tintColor = UISearchBar.appearance().tintColor
}
or whichever color you decide.
Okay, I solved it in a very ugly fix. I would recommend something else but after googling for hours I don't know how.
I'm changing the tint-color and wait 1 second and then change it back. That's enough to remove the animation.
// Hides the movement of the cursor in the text field
let originalSearchBarTintColor = textFieldInsideSearchBar?.tintColor
textFieldInsideSearchBar?.tintColor = UIColor.clear
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
textFieldInsideSearchBar?.tintColor = originalSearchBarTintColor
}
Again, I would not recommend this temporary fix as a solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With