首页 > 移动平台 > 详细

IOS键盘挡住UITextView的解决方案

时间:2014-12-04 13:58:58      阅读:195      评论:0      收藏:0      [点我收藏+]
- (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的父控件的framebubuko.com,布布扣

_inputDiaryView就是UITextView的父控件

IOS键盘挡住UITextView的解决方案

原文:http://blog.csdn.net/fanxiaoxuan1234/article/details/41722943

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!