评论功能,点击textView,弹出键盘,textView上移相同高度

//_commentView未textview,需要跟随键盘移动的控件,有疑问可留言

 

//viewDidload中

 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];

#pragma mark --开始编辑

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView

{

    [_commentView becomeFirstResponder];

    return YES;

}

 

#pragma mark --键盘的高度计算:

-(CGFloat)keyboardEndingFrameHeight:(NSDictionary *)userInfo//计算键盘的高度

{

    CGRect keyboardEndingUncorrectedFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];

    CGRect keyboardEndingFrame = [self.view convertRect:keyboardEndingUncorrectedFrame fromView:nil];

    return keyboardEndingFrame.size.height;

}

#pragma mark --根据键盘高度将当前视图向上滚动同样高度。

-(void)keyboardWillAppear:(NSNotification *)notification

{

    CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];

    

    [self.view bringSubviewToFront:_commentView];

    _commentView.transform = CGAffineTransformMakeTranslation(0, -change);

}

#pragma mark --当键盘消失后,视图需要恢复原状。

-(void)keyboardWillDisappear:(NSNotification *)notification

{

    _commentView.transform = CGAffineTransformIdentity;

}

 

posted @ 2017-03-16 14:30  昊天科技  阅读(120)  评论(0编辑  收藏  举报