UITextField文本跳转 [英] UITextField text jumps
问题描述
我有 ViewController
,带有2 UITextField
元素:登录名和密码。我为这些字段设置了委托,其中包括以下代码:
I have ViewController
with 2 UITextField
elements: Login and Password. I set delegate for these fields, which includes code below:
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField === self.loginField {
self.loginField.resignFirstResponder()
self.passwordField.becomeFirstResponder()
return false
}
return true
}
此逻辑应该从登录文本切换用户在键盘上按下下一步按钮时输入密码字段。但我坚持不懈:在
This logic should switch user from login text field to password when he presses Next button on keyboard. But I stuck with glitch: after
self.passwordField.becomeFirstResponder()
登录字段中的文本跳转到左上角和后面。而且更奇怪的是:这个故障只是第一次重现,然后你需要重新创建 ViewController
来观察这种行为
text in login field jumps to the top left corner and back. And what's more strange: this glitch reproduces only first time, then you need recreate ViewController
to observe this behavior
这里是故障的视频 http://tinypic.com/player .php?v = 6nsemw%3E& s = 8#.VgVb3cuqpHx
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField === self.loginField {
self.loginField.resignFirstResponder()
// Shitty workaround. Hi, Apple!
self.loginField.setNeedsLayout()
self.loginField.layoutIfNeeded()
self.passwordField.becomeFirstResponder()
return false
}
return true
}
推荐答案
基于此处发布的一些其他想法,这是一个易于实施的解决方案,在所有情况下(对我而言)都有效,并且似乎没有任何副作用:
Based on some of the other ideas posted here, this is a solution that is easy to implement, works (for me) in all cases, and doesn't appear to have any side effects:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
// Workaround for the jumping text bug.
[textField resignFirstResponder];
[textField layoutIfNeeded];
}
如果您以编程方式从 -textFieldShouldReturn:
或者如果用户只是触及另一个响应者。
This solution works both if you're going to the next field programmatically from -textFieldShouldReturn:
or if the user just touches another responder.
这篇关于UITextField文本跳转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!