[英]Showing iOS onscreen keyboard without a TextField
我有一個特殊的 iPad 應用程序,即使沒有 UITextField 作為第一響應者,屏幕鍵盤也必須保持可見。
據說,出現的鍵盤是由 UITextInputTraits 控制的。
所以我讓我的 ViewController 實現了 UITextInputTraits 協議並將這些方法添加到它:
- (UITextAutocapitalizationType) autocapitalizationType { return UITextAutocapitalizationTypeNone; }
- (UITextAutocorrectionType) autocorrectionType { return UITextAutocorrectionTypeNo; }
- (UITextSpellCheckingType) spellCheckingType { return UITextSpellCheckingTypeNo; }
- (UIKeyboardAppearance) keyboardAppearance { return UIKeyboardAppearanceDefault; }
- (UIKeyboardType) keyboardType { return UIKeyboardTypeAlphabet; }
- (UIReturnKeyType) returnKeyType { return UIReturnKeyDefault; }
- (BOOL) enablesReturnKeyAutomatically { return NO; }
- (BOOL) secureTextEntry { return NO; }
最后,在控制器的viewDidAppear
方法中,我調用了[self becomeFirstResponder]
但這顯然不足以調出鍵盤。 我錯過了什么?
我什至會調用我的keyboardAppearance
方法,但無論如何鍵盤都沒有出現(或在它出現時消失)。
我想知道我是否必須實現另一種協議,例如用於從鍵盤接收輸入的協議。 那會是哪一個?
事實證明,僅實現 UITextInputTraits 是不夠的,但需要實現 UITextInput 協議,以便可以對鍵盤上的輸入做出反應。
在我的視圖控制器的接口聲明中將實現的協議更改為UITextInput
並添加這些屬性后,鍵盤出現 - 僅用於鍵入需要實現的更多方法:
@property (nonatomic, readonly) UITextRange *markedTextRange;
@property (readwrite, copy) UITextRange *selectedTextRange;
使用becomeFirstResponder
方法從視圖控制器顯示鍵盤的更快解決方案是實現UIKeyInput
協議並在視圖控制器中為-(BOOL)canBecomeFirstResponder
返回YES
。 在這種情況下,將使用UIKeyInput
協議方法來處理鍵盤操作。
還要小心,僅在需要避免奇怪行為時才為canBecomeFirstResponder
返回YES
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.