键盘出现时,如何将我的视图稍微向上拖动? [英] How to Drag my view a little bit up when keyboard comes up?

查看:45
本文介绍了键盘出现时,如何将我的视图稍微向上拖动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我有一些通过键盘填充的文本字段.其中有6个,最后两个在屏幕的最下方,每当键盘出现时它就会被隐藏.有什么建议吗?

In my app i have some textfields which gets filled through the keyboard. There are 6 of them and the last two are in very bottom of the screen and it gets hidden whenever keyboard comes up. Any suggestions??

推荐答案

将textFields委托设为self,然后将以下代码添加到textFieldDidBeginEditing和textFieldDidEndEditing.

Make the textFields delegate as self and add the following code to the textFieldDidBeginEditing and textFieldDidEndEditing.

定义以下常量:(已为iPad设置了这些常量.为iphone对其进行了调整)

Define the following constants: (the constants have been set for iPad. Tweak them for iphone)

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 = 302;

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

Add two variables in the .h file of your class. CGFloat animatedDistance; and 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;
}

这篇关于键盘出现时,如何将我的视图稍微向上拖动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆