繁体   English   中英

显示没有 TextField 的 iOS 屏幕键盘

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM