UITextView用來進行文本輸入。
方法becomeFirstResponder和resignFirstResponder可以用來進行彈出系統鍵盤和收下系統鍵盤。
彈出系統鍵盤,文本輸入框獲得焦點。收下系統鍵盤,文本輸入框失去焦點。那么,問題來了。
某個條件下,如點擊界面上的某個按鈕,展示自定義鍵盤。再次點擊,切換到系統鍵盤。
先收下系統鍵盤,再展示自定義鍵盤。比如移動自定義鍵盤View的Frame.Y。但這時因為收下系統鍵盤,本文輸入框失去焦點。
雖然展示了自定義鍵盤。但用戶體驗很不好。光標沒有閃動。自定義鍵盤輸入的內容無法准確插入到文本輸入框里。
解決方法:(見iOS開發文檔)
@property(readwrite, retain) UIView *inputView
If the value in this property is nil, the text view displays the standard system keyboard when it becomes first responder. Assigning a custom view to this property causes that view to be presented instead.
The default value of this property is nil.
這個屬性就是說
1.為nil時展示系統鍵盤。
2.為自定義View時展示自定義鍵盤。
注意要刷新。
- (void)reloadInputViews
You can use this method to refresh the custom input view or input accessory view associated with the current object when it is the first responder. The views are replaced immediately—that is, without animating them into place. If the current object is not the first responder, this method has no effect.
完美解決問題。
文本輸入框一直獲得焦點。自定義鍵盤和系統鍵盤自由切換。