码迷,mamicode.com
首页 > 移动开发 > 详细

IOS键盘挡住UITextView的解决方案

时间:2014-12-04 14:00:20      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   os   for   on   2014   log   

- (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的解决方案

标签:blog   http   io   ar   os   for   on   2014   log   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!