首页 > 其他 > 详细

UIScrollview 键盘遮挡问题

时间:2015-01-22 20:21:20      阅读:222      评论:0      收藏:0      [点我收藏+]
#pragma mark - UIKeyboard Obscure Problem

- (void)handleKeyboardStuff {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapScrollView:)];
    
    [_scrollView addGestureRecognizer:tap];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    
    [self adjustInsetForKeyboardShow:YES notification:notification];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    
    [self adjustInsetForKeyboardShow:NO notification:notification];
}

- (void) tapScrollView:(id)sender {

    [_scrollView endEditing:YES];
}

- (void)adjustInsetForKeyboardShow:(BOOL)show notification:(NSNotification *)notification {

    NSDictionary *userInfo = notification.userInfo;
    CGRect keyboardFrame = ((NSValue *)userInfo[UIKeyboardFrameBeginUserInfoKey]).CGRectValue;
    CGFloat adjustmentHeight = (CGRectGetHeight(keyboardFrame) + 20.0) * (show ? 1 : -1);
    
    UIEdgeInsets scrollInsets = _scrollView.contentInset;
    scrollInsets.bottom += adjustmentHeight;
    _scrollView.contentInset = scrollInsets;

    UIEdgeInsets indicatorInsets = _scrollView.scrollIndicatorInsets;
    indicatorInsets.bottom += adjustmentHeight;
    _scrollView.scrollIndicatorInsets = indicatorInsets;
    
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


添加以上代码,最后在viewDidLoad方法里添加:

[self handleKeyboardStuff];


UIScrollview 键盘遮挡问题

原文:http://blog.csdn.net/tounaobun/article/details/43022945

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