美文网首页
键盘挡住输入框问题

键盘挡住输入框问题

作者: 小小码农1 | 来源:发表于2018-04-10 12:00 被阅读0次

    @property (nonatomic,assign)float currentf_heihgt; //当前tf的高度

    @property (nonatomic,assign)float currentf_y;  //当前tf的y值,方便获得keyborard移动

    #define FUll_VIEW_WIDTH ([[UIScreen mainScreen] bounds].size.width)

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

        UIWindow * window=[[[UIApplication sharedApplication] delegate] window];

        CGRect rect=[textField convertRect: textField.bounds toView:window];

        _currentf_y = rect.origin.y;

        _currentf_heihgt = textField.frame.size.height;

      return YES;

    }

    -(void)keyboardWillShow:(NSNotification*)aNotification {

        //获取键盘的高度

        NSDictionary *userInfo = [aNotification userInfo];

        NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

        CGRect keyboardRect = [aValue CGRectValue];

      CGFloat keyboardheight = keyboardRect.size.height;

        //计算出键盘顶端到inputTextView panel底端的距离(加上自定义的缓冲距离INTERVAL_KEYBOARD)

        //如果大于0,则键盘将输入框遮挡,需调节高度,小于0,则不需要调节

        CGFloat offset = _currentf_y + _currentf_heihgt - (FUll_VIEW_HEIGHT - keyboardheight);

        //取得键盘的动画时间,这样可以在视图上移的时候更连贯

        double duration = [[aNotification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

        //将视图上移计算好的偏移

        NSLog(@"........%f",offset);

        if(offset > 0) {

            [UIView animateWithDuration:duration animations:^{

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

            }];

            NSLog(@"hhhahjfjhsffksfkjs");

        }

    }

    -(void)keyboardwillHiden:(NSNotification*)notification{

      double duration=  [[[notification userInfo]objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue];

        [UIView animateWithDuration:duration animations:^{

            blackGroundView.frame = CGRectMake(0.0f,NAVIGATION_BAR_HEIGHT, blackGroundView.frame.size.width, blackGroundView.frame.size.height);

        }];

    }

    相关文章

      网友评论

          本文标题:键盘挡住输入框问题

          本文链接:https://www.haomeiwen.com/subject/juighftx.html