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

查看:30
本文介绍了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天全站免登陆