iPhone键盘覆盖UITextField [英] iPhone Keyboard Covers UITextField

查看:100
本文介绍了iPhone键盘覆盖UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,在 Interface Builder 中,我设置了一个 UIView ,它在视图底部附近有一个文本字段。当我运行应用程序并尝试在该字段中输入文本时,键盘会向上滑动到字段上方,所以在我再次隐藏键盘之前,我无法看到我正在键入的内容。

I have an app where, in Interface Builder, I set up a UIView that has a text field near the bottom of the view. When I run the app and try to enter text into that field, the keyboard slides up overtop of the field so I can't see what I'm typing until I hide the keyboard again.

有没有其他人遇到这个问题并找到了解决问题的好方法,既没有使父视图可滚动,也没有将文本字段移动到更远的屏幕上?

Has anyone else run into this problem and found a good way to solve it without either making the parent view scrollable or moving the text field farther up the screen?

推荐答案

通常的解决方案是用动画向上滑动字段(及其上方的所有内容),然后在完成后退回。您可能需要将文本字段和其他一些项目放入另一个视图中,并将视图作为一个单元滑动。 (我将这些东西称为板块,如构造板块,但那只是我)。但是如果你不需要花哨的话,这就是一般的想法。

The usual solution is to slide the field (and everything above it) up with an animation, and then back down when you are done. You may need to put the text field and some of the other items into another view and slide the view as a unit. (I call these things "plates" as in "tectonic plates", but that's just me). But here is the general idea if you don't need to get fancy.

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self animateTextField: textField up: NO];
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    const int movementDistance = 80; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed

    int movement = (up ? -movementDistance : movementDistance);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

这篇关于iPhone键盘覆盖UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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