Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove UITextField cursor animation in iOS 11

Tags:

ios

swift

ios11

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.

like image 782
jocken Avatar asked Sep 22 '17 15:09

jocken


2 Answers

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

like image 90
Bruno Cunha Avatar answered Sep 28 '22 06:09

Bruno Cunha


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.

like image 20
jocken Avatar answered Sep 28 '22 06:09

jocken