国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > UIScrollview 键盘遮挡问题

UIScrollview 键盘遮挡问题

来源:程序员人生   发布时间:2015-01-29 08:15:17 阅读次数:3560次
#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 : ⑴); 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];


生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生