Animating UITextInput's textInputView

2019-04-28 07:48发布

问题:

UIKit text input components, such as UITextView and UITextField have a property inputView to add a custom keyboard. There are two questions I have relating to this.

  1. If the keyboard is currently visible and the property is set to a new input view, nothing happens. Resigning and regaining first responder status refreshes the input and displays the new view. Is this the best way to do it? If so it might answer my bigger question:

  2. Is it possible to animate the transition between two input views?

回答1:

From the UIResponder docs:

Responder objects that require a custom view to gather input from the user should redeclare this property as readwrite and use it to manage their custom input view. When the receiver subsequently becomes the first responder, the responder infrastructure presents the specified input view automatically. Similarly, when the view resigns its first responder status, the responder infrastructure automatically dismisses the specified view.

So unfortunately the answer to 1 is Yes and 2 is No.



回答2:

  1. Actually there is a method to do it cleanly: UIResponder's reloadInputViews, available from iOS 3.2!

  2. I think you can animated it with some extra work:

    • Create a clear background window of a higher UIWindowLevel than the keyboard window.
    • Add your custom keyboard there and animate its frame into place.
    • Then set it as your text input's inputView and refresh the first responder as you do.

Your custom keyboard will change its parent view from your custom window to the keyboard one, but hopefully the user won't notice ;)