输入框挡住键盘的解决办法

只需要在- (void)textFieldDidBeginEditing:(UITextField *)textField,- (void)textFieldDidEndEditing:(UITextField *)textField这两个方法里面调用-(void)moveView:(UITextField *)textField leaveView:(BOOL)leave这个方法就可以了


- (void)textFieldDidBeginEditing:(UITextField *)textField

{

    [self moveView:textField leaveView:NO];

}


- (void)textFieldDidEndEditing:(UITextField *)textField

{

    [self moveView:textField leaveView:YES];

}


-(void)moveView:(UITextField *)textField leaveView:(BOOL)leave

{

    UIView *accessoryView = textField.inputAccessoryView;

    UIView *inpuview = textField.inputView;

    int textFieldY = 0;

    int accessoryY = 0;

    if (accessoryView&&inpuview) {

        CGRect accessotyRect = accessoryView.frame;

        CGRect inputViewRect = inpuview.frame;

        accessoryY = 480 - accessotyRect.size.height+inputViewRect.size.height;

    }

    else if (accessoryView)

    {

        CGRect accessotyRect = accessoryView.frame;

        accessoryY = 480 - (accessotyRect.size.height+216);

    }

    else if (inpuview)

    {

        CGRect inputViewRect = inpuview.frame;

        accessoryY = 480 - inputViewRect.size.height;

    }

    else

    {

        accessoryY = 264;//480-216

    }

    

    CGRect textFieldRect = textField.frame;

    textFieldY = textFieldRect.origin.y + textFieldRect.size.height + 20;

    

    int offsetY = textFieldY - accessoryY;

    if (!leave&&offsetY>0) {

        int y_offset = -5;

        y_offset += -offsetY;

        CGRect viewFrame = self.view.frame;

        viewFrame.origin.y += y_offset;

        

        [UIView beginAnimations:nil context:nil];

        [UIView setAnimationBeginsFromCurrentState:YES];

        [UIView setAnimationDuration:0.3];

        [self.view setFrame:viewFrame];

        [UIView commitAnimations];

    }

    else

    {

        CGRect viewFrame = CGRectMake(0, 20, 320, 460);

        [UIView beginAnimations:nil context:NULL];

        [UIView setAnimationBeginsFromCurrentState:YES];

        [UIView setAnimationDuration:0.3];

        [self.view setFrame:viewFrame];

        [UIView commitAnimations];

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值