且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iPhone 键盘覆盖 UITextField

更新时间:2023-09-27 22:48:46

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

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];
}