如何在iPhone上输入文本时显示UITextfield的键盘顶部?

3
我有一个UITextfield,我正在其中输入值。我不想自己滚动或调整keyboard,我希望Keyboard在我输入UITextfield中的文本时自动调整,并始终显示在textfield下面。
欢迎专家提供任何提示。

你尝试过将整个视图向上动画吗? - Shashank Kulshrestha
可能是重复问题 https://dev59.com/u3M_5IYBdhLWcg3wvV5w - Exploring
3个回答

1
这里是我曾经使用过的一些代码。基本上,我们要做的就是在UITextField获得焦点时动画化视图的位置。为了实现这一点,我们必须将UIViewController设为UITextFields的委托。

在文件的.h中定义一个变量float animatedDistance

 @interface <ClassName>

    float animatedDistance;

 @end

 static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
 static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
 static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
 static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
 static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;

 - (void)textFieldDidBeginEditing:(UITextField *)textField
 {
     CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
     CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];

     CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
     CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
     CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
     CGFloat heightFraction = numerator / denominator;

     if (heightFraction < 0.0)
     {
         heightFraction = 0.0;
     }
     else if (heightFraction > 1.0)
     {
         heightFraction = 1.0;
     }

     UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
     if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
     {
         animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
     }
     else
     {
         animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
     }

     CGRect viewFrame = self.view.frame;
     viewFrame.origin.y -= animatedDistance;

     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationBeginsFromCurrentState:YES];
     [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

     [self.view setFrame:viewFrame];

     [UIView commitAnimations];
     }
 }

既然在选择UITextField时视图已经被“推上去”了,我们希望确保在完成后将其向下滑动:

 - (void) textFieldDidEndEditing:(UITextField *)textField
 {
     CGRect viewFrame = self.view.frame;
     viewFrame.origin.y += animatedDistance;

     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationBeginsFromCurrentState:YES];
     [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

     [self.view setFrame:viewFrame];

     [UIView commitAnimations];
 }

欲了解更多信息,请访问苹果文档


在.h文件中定义浮点型变量animatedDistance。 - Shashank Kulshrestha
animatedDistance是一个数值,它将通过距离来使你的视图动画化。 - Shashank Kulshrestha
非常棒的代码先生。非常感谢... 代码运行良好... 正是我想要的。非常感谢您先生... - Areeba Khan
没问题,如果需要其他的东西,请告诉我。 - Shashank Kulshrestha

0

当文本框开始编辑时,动画显示包含文本框的视图的框架(viewcontroller.view.frame.origin.y - 215),并在完成后返回原位。

使用UITextfieldDelegate方法。


0

您可以使用this类来帮助您完成所需的操作。您需要做的就是将UITableView的类设置为TPKeyboardAvoidingTableView。UIScrollView和UICollectionView同理。您可以使用这些类来避免键盘干扰。

希望这能帮到您。如果有帮助,请接受。;) 谢谢,祝好。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接