【问题标题】:textFieldDidBeginEditing is not Being call after animating View动画视图后未调用 textFieldDidBeginEditing
【发布时间】:2014-03-25 06:30:05
【问题描述】:

我在UIScrollView 上动态创建textFields,如果键盘隐藏它,我正在动画视图将文本字段置于键盘上方。动画视图使用下面给出的代码:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect textFiledFrame = textField.frame;

    if (textFiledFrame.origin.y > 219 && textField.tag > 150 && viewAnimated == 0) {
        CGRect superViewFrame = textField.superview.frame;
        superViewFrame.origin.y = superViewFrame.origin.y - 120;
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.6];
        [textField.superview setFrame:superViewFrame];
        [UIView commitAnimations];
        viewAnimated = 1;
    }
}

每次我点击 textFields textFieldDidBeginEditing:(UITextField *)textField 都会被调用,但是一旦视图被动画化,textFieldDidBeginEditing:(UITextField *)textField 不会被调用

【问题讨论】:

  • 尚未尝试您的代码,但 Rect 是关键字。尝试重构它。

标签: ios iphone objective-c animation uiscrollview


【解决方案1】:

您的代码有几个问题:

  • 您不应硬编码任何与键盘相关的尺寸。而是利用 iOS 框架为您提供的键盘信息(参见例如:How to make a UITextField move up when keyboard is present?

  • 您确实在 if 语句中设置了 ViewAnimated = 1 - 您是否曾经将其设为 0?如果不是,则永远不会再次输入 if 语句,因为您在 if 子句中检查 ViewAnimated == 0 ...

  • 变量不应以大写字母开头(Rect、ViewAnimated) - textRect en viewAnimated 更好

【讨论】:

  • 是的,我将 ViewAnimated 向下移动后设为 0。在我的应用程序中,关于 textFields 的所有信息都来自网络,对于同一个 Srollview,textFields 的数量可以是 2,也可以是 200。这就是我使用这个逻辑的原因。至于这个问题我已经解决了,我会给出答案,我会接受它。
  • 你真的不应该那样做 - 查看我提供的链接,并检查使用 contentInsets 滚动视图的答案,这是最好的方法。你不需要摆弄布尔值和框架等来做到这一点,这总是容易出错。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 2012-07-11
  • 1970-01-01
  • 2014-06-29
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
相关资源
最近更新 更多