在 iOS 11 中删除 UITextField 光标动画

Remove UITextField cursor animation in iOS 11

UITextField 上调用 becomeFirstResponder() 时,光标从左上角开始动画。我该如何删除它?

UITextField 在 SearchBar 中。

好的,我用一个非常丑陋的修复程序解决了它。我会推荐其他东西,但谷歌搜索了几个小时后我不知道如何。

我正在更改 tint-color 并等待 1 秒钟,然后再将其更改回来。这足以删除动画。

// 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
            }

同样,我不推荐将此临时修复作为解决方案。

[SWIFT 4]

根据上述答案,我们可以制定另一种解决方法,即禁用此本机光标进入动画。

让我们观察键盘事件,例如:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: .UIKeyboardDidShow, object: nil)

然后,在每个方法上:

@objc func keyboardWillShow(_ notification: Notification) {
    searchBar.tintColor = UIColor.clear
}

@objc func keyboardDidShow(_ notification: Notification) {
    searchBar.tintColor = UISearchBar.appearance().tintColor
}

或您决定的颜色。