IOS键盘挡住UITextView的解决方案

- (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)unregisterForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [self registerForKeyboardNotifications];
    _viewFrame = _inputDiaryView.frame;
}

- (void)viewDidDisappear:(BOOL)animated {
    [self unregisterForKeyboardNotifications];
}

- (void)keyboardWasShown:(NSNotification *)aNotification {
    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView beginAnimations:TEXTVIEW_KEYBOARD context:nil];
    [UIView setAnimationDuration:animationDuration];
    _inputDiaryView.frame = CGRectMake(0, _viewFrame.origin.y - keyboardRect.size.height, _viewFrame.size.width, _viewFrame.size.height);
    [UIView commitAnimations];
}

- (void)keyboardWillBeHidden:(NSNotification *)aNotification{
    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView beginAnimations:TEXTVIEW_KEYBOARD context:nil];
    [UIView setAnimationDuration:animationDuration];
    _inputDiaryView.frame = _viewFrame;
    [UIView commitAnimations];
}

_viewFrame:是UITextView的父控件的frame

_inputDiaryView就是UITextView的父控件

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。