Iphone 当键盘打开时,如何将我的视图向上拖动一点?

Iphone 当键盘打开时,如何将我的视图向上拖动一点?,iphone,uiview,Iphone,Uiview,在我的应用程序中,我有一些通过键盘填充的文本字段。其中有6个,最后两个在屏幕的最底部,每当键盘出现时,它就会被隐藏起来。 任何建议???使用UIScrollview以便用户可以向下滚动。您可以使用UITextField的inputAccessoryView来显示“下一个”和“上一个”按钮,单击“让输入跳转到下一个文本字段并滚动到它” 看看这个,同样的问题,不同的解决方案。将textFields委托为self,并将以下代码添加到textfielddebeginediting和textfieldde

在我的应用程序中,我有一些通过键盘填充的文本字段。其中有6个,最后两个在屏幕的最底部,每当键盘出现时,它就会被隐藏起来。
任何建议???

使用UIScrollview以便用户可以向下滚动。您可以使用UITextField的inputAccessoryView来显示“下一个”和“上一个”按钮,单击“让输入跳转到下一个文本字段并滚动到它”


看看这个,同样的问题,不同的解决方案。

将textFields委托为self,并将以下代码添加到textfielddebeginediting和textfielddendediting

定义以下常量:(这些常量是为iPad设置的。为iphone调整它们)

在类的.h文件中添加两个变量。CGFloat距离;和UITextField*currentField

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    currentField = textField;
    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];

    CGRect textFieldRect =[self.view.window convertRect:textField.bounds fromView:textField];

    CGRect viewRect =[self.view.window convertRect:self.view.bounds fromView:self.view];
    if (orientation == UIInterfaceOrientationLandscapeLeft )
    {
        textFieldRect.origin.y = textFieldRect.origin.x;
        textFieldRect.size.height = textFieldRect.size.width;
        viewRect.size.height = viewRect.size.width;
    }
    else  if (orientation == UIInterfaceOrientationLandscapeRight )
    {
        textFieldRect.origin.y =viewRect.size.width - textFieldRect.origin.x;
        textFieldRect.size.height = textFieldRect.size.width;
        viewRect.size.height = viewRect.size.width;
    }
    else   if (orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        textFieldRect.origin.y = viewRect.size.height - textFieldRect.origin.y;
    }

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

}


- (void)textFieldDidEndEditing:(UITextField *)textField {

    [textField resignFirstResponder];

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

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

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];

    currentField = nil;
}
-(void)textFieldDidBeginEditing:(UITextField*)textField{
currentField=textField;
界面定向=
[[UIApplication sharedApplication]statusBarOrientation];
CGRect textFieldRect=[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect=[self.view.window convertRect:self.view.bounds fromView:self.view];
if(方向==UIInterfaceOrientationAndscapeLeft)
{
textFieldRect.origin.y=textFieldRect.origin.x;
textFieldRect.size.height=textFieldRect.size.width;
viewRect.size.height=viewRect.size.width;
}
else if(方向==UIInterfaceOrientationAndscapeRight)
{
textFieldRect.origin.y=viewRect.size.width-textFieldRect.origin.x;
textFieldRect.size.height=textFieldRect.size.width;
viewRect.size.height=viewRect.size.width;
}
else if(方向==UIInterfaceOrientationGraphitalUpsideDown)
{
textFieldRect.origin.y=viewRect.size.height-textFieldRect.origin.y;
}
CGFloat midline=textFieldRect.origin.y+0.5*textFieldRect.size.height;
CGFloat分子=中线-viewRect.origin.y-最小滚动分数*viewRect.size.height;
CGFloat分母=(最大滚动分数-最小滚动分数)*viewRect.size.height;
CGFloat heightFraction=分子/分母;
如果(高度分数<0.0)
{
高度分数=0.0;
}
否则如果(高度分数>1.0)
{
高度分数=1.0;
}
如果(方向==UIInterfaceOrientationParative||
方向==UIInterfaceOrientationGraphitalUpsideDown)
{
动画距离=地板(纵向\键盘\高度*高度分数);
}
其他的
{
动画距离=地板(横向键盘高度*高度分数);
}
CGRect viewFrame=self.view.frame;
viewFrame.origin.y-=动画距离;
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationBeginsFromCurrentState:是];
[UIView设置动画持续时间:键盘动画持续时间];
[self.view setFrame:viewFrame];
[UIView委员会];
}
-(void)textfielddidediting:(UITextField*)textField{
[textField resignFirstResponder];
CGRect viewFrame=self.view.frame;
viewFrame.origin.y+=动画距离;
动画距离=0;
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationBeginsFromCurrentState:是];
[UIView设置动画持续时间:键盘动画持续时间];
[self.view setFrame:viewFrame];
[UIView委员会];
currentField=零;
}

它给了我一个错误,当前字段未声明,动画距离未声明。这些是什么?哦,对不起。您必须声明CGFloat animatedDistance;和UITextField*currentField;在你们班的.h。我来编辑答案。谢谢你指出这一点。或者看看Matt的这篇很棒的教程:参见带有代码示例的分步博客教程。。
- (void)textFieldDidBeginEditing:(UITextField *)textField {

    currentField = textField;
    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];

    CGRect textFieldRect =[self.view.window convertRect:textField.bounds fromView:textField];

    CGRect viewRect =[self.view.window convertRect:self.view.bounds fromView:self.view];
    if (orientation == UIInterfaceOrientationLandscapeLeft )
    {
        textFieldRect.origin.y = textFieldRect.origin.x;
        textFieldRect.size.height = textFieldRect.size.width;
        viewRect.size.height = viewRect.size.width;
    }
    else  if (orientation == UIInterfaceOrientationLandscapeRight )
    {
        textFieldRect.origin.y =viewRect.size.width - textFieldRect.origin.x;
        textFieldRect.size.height = textFieldRect.size.width;
        viewRect.size.height = viewRect.size.width;
    }
    else   if (orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        textFieldRect.origin.y = viewRect.size.height - textFieldRect.origin.y;
    }

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

}


- (void)textFieldDidEndEditing:(UITextField *)textField {

    [textField resignFirstResponder];

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

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

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];

    currentField = nil;
}