ios - 显示没有 TextField 的 iOS 屏幕键盘

标签 ios iphone keyboard

我有一个特殊的 iPad 应用程序,即使没有 UITextField 作为第一响应者,屏幕键盘也必须保持可见。

据说键盘的出现是由UITextInputTraits控制的。

所以我让我的 ViewController 实现了 UITextInputTraits 协议(protocol)并向其中添加了这些方法:

- (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; }

最后,在 Controller 的 viewDidAppear 方法中,我调用 [self comeFirstResponder]

但这显然不足以调出键盘。我错过了什么?

我什至确实收到了对我的 keyboardAppearance 方法的调用,但键盘无论如何都不会出现(或在启动时消失)。

我想知道我是否必须实现另一个协议(protocol),例如一个用于接收来自键盘的输入。那会是哪一个?

最佳答案

事实证明,实现 UITextInputTraits 还不够,还需要实现 UITextInput 协议(protocol),以便能够对键盘上的输入使用react。

将 View Controller 的接口(interface)声明中实现的协议(protocol)更改为 UITextInput 并添加这些属性后,会出现键盘 - 仅用于键入需要实现的更多方法:

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

关于ios - 显示没有 TextField 的 iOS 屏幕键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22583029/

相关文章:

iphone - 防止双击 UITabBarController 时自动 popToRootViewController

ios - 在 TextField 中输入文本时不显示键盘

android - 如何制作适用于 Android 的曲面屏幕键盘

ios - 可以在后台线程上从 ASIHTTPRequest/AFNetworking 执行 completionBlock 吗?

ios - iOS 9.2 上的游戏中心崩溃应用程序

ios - 在 iOS 中进行复制拟合的最佳方法?

cordova - ionic Cordova 键盘将 ionic 页脚栏推离屏幕

ios - 如何在源代码控制下将本地化添加到设置包

iphone - 如何访问 View Controller 的父级并正确关闭 self 和 self.parent?

iphone - 使用分段控件在 2 个 View 之间切换