如何使在Cell中的TextFiled(TextView)自适应键盘高度
来源:互联网 发布:前两年悲伤的网络歌曲 编辑:程序博客网 时间:2024/06/05 21:12
首先必须在页面显示时加上键盘通知事件
- (void)viewWillAppear:(BOOL)animated{ // 设置键盘显示和隐藏通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];}当然不要忘记在页面显示时把事件注销掉
- (void)viewWillDisappear:(BOOL)animated{ // 注销键盘显示和隐藏通知 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];}
因为需要让TableView的显示区域缩小,所以要要使得TableView可以滑动,顺便看到滚动条
_tableView.scrollEnabled = YES;
然后就是键盘的通知事件了
因为需要动画与键盘弹上同步所以需要键盘弹上的时间,同时获取键盘的Rect
CGFloat duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; CGRect bounds = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
然后计算键盘的偏移量UIEdgeInsets
如果你的TableView的底部离View的还有距离,则需要见键盘的高度上减去此距离
NSInteger distance = 13; UIEdgeInsets e = UIEdgeInsetsMake(0, 0, bounds.size.height-distance, 0);
再加上动画效果
[UIView animateWithDuration:duration animations:^{ [_tableView setScrollIndicatorInsets:e]; [_tableView setContentInset:e]; }];那么TableView的显示区域就设置好了,当然在取消键盘的时候TableView需要复原
-(void)keyboardWillHide:(NSNotification *)notification{ CGFloat duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; [UIView animateWithDuration:duration animations:^{ [_tableView setScrollIndicatorInsets:UIEdgeInsetsZero]; [_tableView setContentInset:UIEdgeInsetsZero]; }]; _tableView.scrollEnabled = NO;}
看看效果发现TableView虽然可视区域正确,但是点击被键盘覆盖部分的Cell中的TextFiled却不会自动上弹,这是需要一个标记你所点击的TextFiled所在Cell的Rect
故此定义一个全局变量
CGRect m_InputRect; // 所选中的TextFiled的Rcct设置textFiled的代理为self,并加上代理UITextFieldDelegate
cell.textContext.delegate = self;实现代理,目的就是找到TextFiled所在的Cell并且取得他的Rect,然后赋值给m_InputRec
<span style="font-size:24px">#pragma mark -UITextFieldDelegate- (void)textFieldDidBeginEditing:(UITextField *)textField{ m_InputRect = CGRectZero; UIView* parent = textField.superview; while (parent) { if ([parent isKindOfClass:[UITableViewCell class]]) { m_InputRect = parent.frame; break; } parent = parent.superview; }}</span>
所以最后在keyboardWillShow中加上
[_tableView scrollRectToVisible:m_InputRect animated:YES];即最后keyboardWillShow的实现为:
#pragma mark - Keyboard Notification-(void)keyboardWillShow:(NSNotification *)notification{ CGFloat duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; CGRect bounds = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; UIEdgeInsets e = UIEdgeInsetsMake(0, 0, bounds.size.height - 13, 0); [UIView animateWithDuration:duration animations:^{ [_tableView setScrollIndicatorInsets:e]; [_tableView setContentInset:e]; [_tableView scrollRectToVisible:m_InputRect animated:YES]; }]; _tableView.scrollEnabled = YES;}
注明:如需转载请注明出处http://blog.csdn.net/dengbin9009/article/details/12706633,另发现不合理的地方还望大家即时指出。
- 如何使在Cell中的TextFiled(TextView)自适应键盘高度
- tableviewcell 自动调整textfiled的键盘遮挡问题 UITableViewController中的tableview做可以自适应键盘高度来改变tableview的额外高度。
- iOS中的cell自适应高度--StoryBorad
- StoryBoard设置Cell中的Label自适应高度
- 在tableview上面的textfiled,还有textview,如何移动。
- IOS研究成果——在autolayout 如何实现 Cell 的高度自适应
- UItableView cell 自适应高度
- iOS Cell高度自适应
- ios cell自适应高度
- iOS8 Cell 高度自适应
- Cell自适应高度
- cell单元格高度自适应
- cell自适应高度
- cell自适应高度
- cell的自适应高度
- cell自适应高度
- Cell自适应高度
- cell高度自适应
- 【Tech-Android-Jni】在Ecplise中配置新的Run命令,用于生成.so文件
- 对as3数组进行group分组操作
- android下截屏
- 常见面试题排序算法(二)
- Bayesain Networks
- 如何使在Cell中的TextFiled(TextView)自适应键盘高度
- [转]ubuntu中eclipse的悬浮提示窗口背景黑色的修改方法
- Linux Framebuffer
- CYUSB 开发包
- jQuery中的.bind()、.live()和.delegate()之间区别分析
- Select语句详细剖析以及其执行顺序
- eclipse上的svn用户信息修改
- 常用的宏定义
- apk反编译步骤