iOS keyboard遮挡textfiled的问题

1.添加delegate,2.设置委托,3.实现以下3个方法:
 

//开始编辑输入框的时候,软键盘出现,执行此事件

-(void)textFieldDidBeginEditing:(UITextField *)textField

{

    CGRect frame = textField.frame;

    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216

    

    NSTimeInterval animationDuration = 0.30f;

    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];

    [UIView setAnimationDuration:animationDuration];

    

    //将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示

    if(offset > 0)

        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);

    

    [UIView commitAnimations];

}

 

//当用户按下return键或者按回车键,keyboard消失

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    return YES;

}

 

//输入框编辑完成以后,将视图恢复到原始状态

-(void)textFieldDidEndEditing:(UITextField *)textField

{

    self.view.frame =CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);

}

 

posted @ 2015-06-30 17:20  tongyuling  阅读(231)  评论(0编辑  收藏  举报