防止键盘遮挡

offset = originYTF + heightTF + heightKeyboard + view.orginY  -   view.height;

1,把内容都放在view中,然后判断会不会遮挡到,会的话向上移动一定距离。

#pragma mark   UITextFieldDelegate


- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    switch (textField.returnKeyType) {

        caseUIReturnKeyNext:

            if(textField == _tfPhone){

                [_tfCompany becomeFirstResponder];

            } else if(textField ==_tfCompany){

                [_tfAddress becomeFirstResponder];

            }else if(textField ==_tfAddress){

                [_tfBrand becomeFirstResponder];

            }else if(textField ==_tfBrand){

                [_tfWays becomeFirstResponder];

            }else if(textField ==_tfWays){

                [_tfPerson becomeFirstResponder];

            }

            break;

        caseUIReturnKeyDone:

            if(textField == _tfPerson){

                NSTimeInterval animationDuration =0.30f;

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

                [UIView setAnimationDuration:animationDuration];

                CGRect rect =CGRectMake(self.view.frame.origin.x,64.0f,self.view.frame.size.width,self.view.frame.size.height);

                self.view.frame = rect;

                [UIView commitAnimations];

                [textField resignFirstResponder];

                [self onSubmit:nil];

            }

            break;

        default:

            break;

    }

    return YES;

}


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

    NSTimeInterval animationDuration = 0.30f;

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

    [UIView setAnimationDuration:animationDuration];

    float offset = textField.frame.origin.y + textField.frame.size.height + self.view.frame.origin.y +320 -self.view.window.frame.size.height;

    if (offset > 0) {

        CGRect rect = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y - offset,self.view.frame.size.width,self.view.frame.size.height);

        self.view.frame = rect;

    }

    [UIView commitAnimations];

}


2,在view中加一个和view一样大小的UIScrollView,在scrollview的滑动的委托方法中隐藏键盘,其他和上面的一样。

#pragma UIScrollViewDelegate


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    [self.tfPhoneresignFirstResponder];

    [self.tfCompanyresignFirstResponder];

    [self.tfAddressresignFirstResponder];

    [self.tfBrandresignFirstResponder];

    [self.tfWaysresignFirstResponder];

    [self.tfPersonresignFirstResponder];

    

    NSTimeInterval animationDuration = 0.30f;

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

    [UIView setAnimationDuration:animationDuration];

    CGRect rect = CGRectMake(self.view.frame.origin.x,64.0f,self.view.frame.size.width,self.view.frame.size.height);

    self.view.frame = rect;

    [UIView commitAnimations];


}


3,在view中加一个和view一样大小的UIScrollView,然后判断会不会遮挡到,会的话把view向上移动一定距离,并且把scrollview的contensize拉长键盘高度。

4,在view中加一个和view一样大小的UIScrollView,然后判断会不会遮挡到,把scrollview的contensize拉长键盘高度,并把scrollview scroll到一定的位置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值