我可以动态更改 UITextField 的属性 UIReturnKeyType 吗?
我们可以将 UITextField 的这个属性设置为“Send”、“Search”、“Go”等:
@property(nonatomic) UIReturnKeyType returnKeyType
但是在使用 UITextField 时设置它根本不会改变。
我想要这种行为: 当UITextField为空时,让returnKeyType为“Return”,否则让returnKeyType为“Send”。
we can set this property of UITextField to "Send", "Search", "Go", etc.:
@property(nonatomic) UIReturnKeyType returnKeyType
But setting it while using the UITextField doesn't change at all.
I want this behavior:
When UITextField is empty, let returnKeyType be "Return", otherwise let returnKeyType be "Send".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通常,在显示键盘时会检查
returnKeyType
和其他属性,但不会监视以后的更新。更改returnKeyType
后,尝试在文本字段上调用 reloadInputViews
,这应该指示它刷新键盘设置。Normally the
returnKeyType
and other properties are checked when the keyboard is displayed, but are not monitored for later updates. Try callingreloadInputViews
on the text field after changing thereturnKeyType
, that should instruct it to refresh the keyboard settings.不幸的是,
-[reloadInputViews]
会弄乱两字节键盘上的键盘输入。双字节键盘(例如汉字)将临时字符放入文本字段中,并要求用户按 Enter 键进行辅助击键以确认输入的字符。调用
-[reloadInputViews]
会放弃辅助击键状态。我还没有找到针对这种情况的解决方案。
Unfortunately,
-[reloadInputViews]
will mess up keyboard input on two-byte keyboards.Two-byte keyboards (Kanji for example) put temporary characters in the text field and require the user to hit enter a secondary keystroke to confirm the character entered. Calling
-[reloadInputViews]
abandons the secondary keystroke state.I haven't yet found a solution for this scenario.
有些人建议在 textField 上调用 reloadInputViews 来强制更新键盘的返回键。不幸的是,当您在文本字段上调用 reloadInputViews 时,键盘会重置,这会将其更改回文本字段的默认值,例如,从数字输入切换到电子邮件输入。您可以通过仅在 returnKeyType 更改时调用 reloadInputViews 来最大程度地减少发生这种情况的频率。
这并不能完全解决问题,但可以将问题最小化到回车键发生变化的一次击键。
Some have proposed calling reloadInputViews on the textField to force the keyboard's return key to update. Unfortunately the keyboard resets when you call reloadInputViews on the textField, which changes it back to the textField's default, e.g., switching from number entry to email entry. You can minimize how often this happens by only calling reloadInputViews IF the returnKeyType changes.
This doesn't completely solve the problem, but minimizes it to the one keystroke where the return key changes.
正如 Animie 所说,最好的方法是
reloadInputViews
告诉键盘有关更改的信息。您可以这样做:
在代码中的某个位置,例如在 viewDidLoad 中,注册
UIKeyboardWillShowNotification
:当键盘出现时,您
重新加载InputViews
:As Animie said, the best way is to
reloadInputViews
to tell the keyboard about the changes.You can do like this:
Somewhere in your code, in viewDidLoad for example, register for
UIKeyboardWillShowNotification
:And when the keyboard shows up, you
reloadInputViews
:正如大家所说,
reloadInputViews
将键盘重置为其初始UIKeyboardType
。 Apple 应该为我们提供一个 API 来询问键盘的键盘类型,以将其设置回重新加载输入视图之前的状态。使用新的 iOS8 API,您可以创建自己的键盘并返回 UIKeyboardType。
如果仍然使用 iOS7,您可以询问某个键盘子视图处于哪种状态。存储该值并在需要时重置。在这里查看我的来源:https://github.com/ustwo/US2KeyboardType
As you all say,
reloadInputViews
resets the keyboard to its initialUIKeyboardType
. Apple should give us an API to ask the keyboard for its keyboard type to set it back to what it was before reloading the input views.With the new iOS8 API you can create your own keyboard returning the UIKeyboardType.
If still using iOS7 you can ask a certain keyboard child view in which state it is. Store that value and reset whenever you need to. Have a look at my source here: https://github.com/ustwo/US2KeyboardType