简体   繁体   English

显示没有 TextField 的 iOS 屏幕键盘

[英]Showing iOS onscreen keyboard without a TextField

I have a special iPad app where the onscreen keyboard must remain visible even if there is no UITextField being the first responder.我有一个特殊的 iPad 应用程序,即使没有 UITextField 作为第一响应者,屏幕键盘也必须保持可见。

Supposedly, the keyboard appearing is controlled by UITextInputTraits.据说,出现的键盘是由 UITextInputTraits 控制的。

So I've made my ViewController implement the UITextInputTraits protocol and added these methods to it:所以我让我的 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; }

Lastly, in the controller's viewDidAppear method I'm invoking [self becomeFirstResponder]最后,在控制器的viewDidAppear方法中,我调用了[self becomeFirstResponder]

But that's apparently not enough to bring up the keyboard.但这显然不足以调出键盘。 What am I missing?我错过了什么?

I do even get a call to my keyboardAppearance method, but the keyboard does not come up (or disappears when it was up) regardless.我什至会调用我的keyboardAppearance方法,但无论如何键盘都没有出现(或在它出现时消失)。

I wonder if I have to implement another protocol, eg one for receiving input from the keyboard.我想知道我是否必须实现另一种协议,例如用于从键盘接收输入的协议。 Which one would that be?那会是哪一个?

Turns out that it's not enough to implement UITextInputTraits but one need to implement the UITextInput protocol so that one can react to typing on the keyboard.事实证明,仅实现 UITextInputTraits 是不够的,但需要实现 UITextInput 协议,以便可以对键盘上的输入做出反应。

After changing the implemented protocol in my view controller's interface declaration to UITextInput , and adding these properties, the keyboard appears - only for typing more methods neede to be implemented:在我的视图控制器的接口声明中将实现的协议更改为UITextInput并添加这些属性后,键盘出现 - 仅用于键入需要实现的更多方法:

@property (nonatomic, readonly) UITextRange *markedTextRange;
@property (readwrite, copy) UITextRange *selectedTextRange;

A quicker solution to show the keyboard from your view controller, using becomeFirstResponder method, is to implement UIKeyInput protocol and return YES for -(BOOL)canBecomeFirstResponder in your view controller.使用becomeFirstResponder方法从视图控制器显示键盘的更快解决方案是实现UIKeyInput协议并在视图控制器中为-(BOOL)canBecomeFirstResponder返回YES UIKeyInput protocol methods will be used in this case for handling keyboard actions.在这种情况下,将使用UIKeyInput协议方法来处理键盘操作。

Also be careful to return YES for canBecomeFirstResponder only when needed to avoid weird behaviour.还要小心,仅在需要避免奇怪行为时才为canBecomeFirstResponder返回YES

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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