根据TextField的高度自动调节弹出键盘高度

    int prewTag ;  //编辑上一个UITextField的TAG,需要在XIB文件中定义或者程序中添加,不能让两个控件的TAG相同

    float prewMoveY; //编辑的时候移动的高度

-(void) textFieldDidBeginEditing:(UITextField *)textField

{

    CGRect textFrame =  textField.frame;

    float textY = textFrame.origin.y+textFrame.size.height;

    float bottomY = self.view.frame.size.height-textY;

    if(bottomY>=216)  //判断当前的高度是否已经有216,如果超过了就不需要再移动主界面的View高度

    {

        prewTag = -1;

        return;

    }

    prewTag = textField.tag;

    float moveY = 216-bottomY;

    prewMoveY = moveY;

    

    NSTimeInterval animationDuration = 0.30f;

    CGRect frame = self.view.frame;

    frame.origin.y -=moveY;//viewY轴上移

    frame.size.height +=moveY; //View的高度增加

    self.view.frame = frame;

    [UIViewbeginAnimations:@"ResizeView"context:nil];

    [UIView setAnimationDuration:animationDuration];

    self.view.frame = frame;

    [UIViewcommitAnimations];//设置调整界面的动画效果

}

 

/**

 结束编辑UITextField的方法,让原来的界面还原高度

 */

-(void) textFieldDidEndEditing:(UITextField *)textField

{

    if(prewTag == -1) //当编辑的View不是需要移动的View

    {

        return;

    }

    float moveY ;

    NSTimeInterval animationDuration = 0.30f;

    CGRect frame = self.view.frame;

    if(prewTag == textField.tag) //当结束编辑的ViewTAG是上次的就移动

    {   //还原界面

        moveY =  prewMoveY;

        frame.origin.y +=moveY;

        frame.size. height -=moveY;

        self.view.frame = frame;

    }

    //self.view移回原位置

    [UIViewbeginAnimations:@"ResizeView"context:nil];

    [UIView setAnimationDuration:animationDuration];

    self.view.frame = frame;

    [UIViewcommitAnimations];

    [textField resignFirstResponder];

    

    

}

posted on 2013-06-03 16:38  晏晟  阅读(164)  评论(0编辑  收藏  举报

导航