即使在 userInteractionEnabled = NO 时,在 firstResponder 上也显示 UITextField 键盘 [英] Show UITextField keyboard on firstResponder even when userInteractionEnabled = NO

查看:31
本文介绍了即使在 userInteractionEnabled = NO 时,在 firstResponder 上也显示 UITextField 键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITextField 作为第一响应者.我想在进入视图时显示键盘,但我想这样做用户将无法编辑它并且光标也将始终隐藏.

I have a UITextField that is first responder. I want to show keyboard when entering the view but I want to do that the user will not be able to edit it and the cursor will be hidden all time as well.

当你点击键盘字母时,它会写在 UITextField 中,但用户将无法在那里编辑任何内容,甚至无法复制.

When you click on a keyboard letter, it will be written in the UITextField, but the user will not be able to edit nothing there, even not to copy.

谢谢!

推荐答案

好吧,根据我的评论,我的解决方案是使用具有 hidden 属性的代理 UITextField设置为 YES.我所做的是将隐藏的文本字段添加到视图中,并在其上调用 becomeFirstResponder.用户不知道此文本字段存在.在来自文本字段的委托回调中,我将用户输入的文本添加到 UITextView(尽管您可以将文本添加到您想要的任何内容,例如 UITextField就像你的问题一样).我关闭了可见文本视图的 userInteractionEnabled.这会产生您想要的效果.

Ok, per my comment, my solution is to have a surrogate UITextField that has its hidden property set to YES. What I do is add that hidden text field to the view, and call becomeFirstResponder on it. The user has no idea this text field exists. In the delegate callback from the text field, I take the text the user typed in and add it to a UITextView (though you could add the text to whatever you wanted, like a UITextField like in your question). I turn off userInteractionEnabled for the visible text view. This creates the effect you desire.

我创建了一个上传到 Github 的示例项目.(如果您不熟悉它,只需单击 zip 按钮下载它,解压缩它,然后打开 .xcodeproj 文件).https://github.com/MaxGabriel/HiddenTextField

I created a sample project that I uploaded to Github. (If you aren't familiar with it, just click the zip button to download it, unzip it, and open the .xcodeproj file). https://github.com/MaxGabriel/HiddenTextField

这篇关于即使在 userInteractionEnabled = NO 时,在 firstResponder 上也显示 UITextField 键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆