Search code examples
iosswiftios11

Remove UITextField cursor animation in iOS 11


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.


Solution

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