防止键盘遮挡

来源:互联网 发布:otc焊接机器人编程ppt 编辑:程序博客网 时间:2024/05/22 01:18

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
原创粉丝点击