首页 > 代码库 > UIScrollview 键盘遮挡问题
UIScrollview 键盘遮挡问题
#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 键盘遮挡问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。