菜鸟教程小白 发表于 2022-12-12 08:52:52

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


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

<p>据说出现的键盘是由 UITextInputTraits 控制的。</p>

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

<pre><code>- (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; }
</code></pre>

<p>最后,在 Controller 的 <code>viewDidAppear</code> 方法中,我正在调用 <code></code></p>

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

<p>我什至接到了对我的 <code>keyboardAppearance</code> 方法的调用,但无论如何键盘都没有出现(或在它启动时消失)。</p>

<p>我想知道我是否必须实现另一个协议(protocol),例如一种用于接收来自键盘的输入。那会是哪一个?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>事实证明,仅实现 UITextInputTraits 是不够的,还需要实现 UITextInput 协议(protocol),以便对键盘上的输入使用react。</p>

<p>在我的 ViewController 的接口(interface)声明中将实现的协议(protocol)更改为<code>UITextInput</code>,并添加这些属性后,键盘出现-仅用于键入需要实现的更多方法:</p>

<pre><code>@property (nonatomic, readonly) UITextRange *markedTextRange;
@property (readwrite, copy) UITextRange *selectedTextRange;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 显示没有 TextField 的 iOS 屏幕键盘,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22583029/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22583029/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 显示没有 TextField 的 iOS 屏幕键盘