键盘挡住输入框问题

@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);



    }];

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容