3

我有一个UITextfield我正在输入的值。我不想滚动自己或调整keyboard自己,我想Keyboard在输入文本时自动调整自己UITextfield并始终显示在textfield.

专家的任何提示都将非常受欢迎。

4

3 回答 3

1

这是我使用过的一些代码。基本上,我们要做的是在UITextField获得焦点时为视图的位置设置动画。为此,我们必须让我们成为我们UIViewController的代表UITextFields

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

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

Now that the view has been 'pushed up' when a UITextFieldis selected, we want to make sure that we slide it back down when we're done:

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

有关更多信息,请访问Apple 文档

于 2013-01-22T12:30:15.467 回答
0

当文本字段开始编辑时,为包含文本字段的视图的框架 (viewcontroller.view.frame.origin.y - 215) 设置动画,并在完成时返回。

使用 UITextfieldDelegate 方法

于 2013-01-22T12:28:10.293 回答
0

你可以使用这个课程来帮助你做需要的事情。您需要做的就是将该 UITableView 的类设为 TPKeyboardAvoidingTableView。UIScrollView 和 UICollectionView 相同。您可以使用这些类来避免使用键盘。

希望这可以帮助。如果是,请接受。;) 谢谢和干杯。

于 2014-02-25T13:41:20.733 回答