iOS TextFiled、TextView 关于键盘的收起以及处理键盘遮挡
来源:互联网 发布:maxdos pxe网络启动版 编辑:程序博客网 时间:2024/05/16 07:36
在iOS开发中,UITextFiled和UITextView是很常见的两个控件,当我们设置好这两个控件后,点击文字输入区域,系统会自动弹出键盘,但是如何收起键盘、点击哪里收起键盘,以及在iPhone4中键盘弹出后遮挡输入框怎么办呢?
这篇文章将带领大家解决:
1》点击其他空白区域收起键盘
2》点击键盘右下角的键收起键盘
3》处理键盘遮挡问题
一,点击其他空白区域收起键盘
- (void)viewDidLoad { [super viewDidLoad]; [self setUpForDismissKeyboard]; }
#pragma mark - 回收任何空白区域键盘事件- (void)setUpForDismissKeyboard { NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; UITapGestureRecognizer *singleTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAnywhereToDismissKeyboard:)]; NSOperationQueue *mainQuene =[NSOperationQueue mainQueue]; [nc addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQuene usingBlock:^(NSNotification *note){ [self.view addGestureRecognizer:singleTapGR]; }]; [nc addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQuene usingBlock:^(NSNotification *note){ [self.view removeGestureRecognizer:singleTapGR]; }];}- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer { //此method会将self.view里所有的subview的first responder都resign掉 [self.view endEditing:YES];}
二,点击键盘右下角的键收起键盘
#pragma mark - TextView 代理方法-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ if ([text isEqualToString:@"\n"]) { [self.workLogTextView resignFirstResponder]; return NO; } return YES;}
注意:需要遵守textView/textFiled的代理。改代码是textView代理方法,若实际用到的是textFiled,只需调用textFiled的该类方法即可。
三,处理键盘遮挡问题
#pragma mark 键盘遮挡- (BOOL)textViewShouldBeginEditing:(UITextView *)textView { if (self.userInfo.isPhone4) { CGFloat offset_y = 0.f; if (textView.tag == CALL_CONTENT_TEXTFIRLD) { offset_y = 100.f; } CGPoint point = self.BackScrollView.contentOffset; point = CGPointMake(point.x, offset_y); [UIView animateWithDuration:0.25 animations:^{ self.BackScrollView.contentOffset = point; }]; } return YES;}- (BOOL)textViewShouldEndEditing:(UITextView *)textView{ if (self.userInfo.isPhone4) { CGFloat offset_y = 0.f; if (textView.tag == CALL_CONTENT_TEXTFIRLD) { offset_y = 100.f; } CGPoint point = self.BackScrollView.contentOffset; point = CGPointMake(point.x, 0); [UIView animateWithDuration:0.25 animations:^{ self.BackScrollView.contentOffset = point; }]; } return YES;}
注意:需要遵守 UIScrollViewDelegate 和 textView/textFiled的代理。需要该页面的父视图是UIScrollView,才能保证弹出键盘时页面向上移动,收起键盘时页面向下移动。代码中的self.BackScrollView就是对应的父视图,使用时请替换掉。
1 0
- iOS TextFiled、TextView 关于键盘的收起以及处理键盘遮挡
- iOS解决键盘遮挡TextFiled的问题
- ios关于键盘遮挡处理
- textFiled 键盘遮挡问题
- IOS 键盘遮挡处理
- iOS 键盘遮挡处理
- iOS 键盘的收起
- 项目总结-关于收起键盘的处理
- ios关于键盘遮挡的简单方法
- iOS CJKeyBoardBarTextField_键盘遮挡处理
- ios 处理键盘遮挡问题
- iOS开发 - 键盘的收起
- textView键盘遮挡问题
- textView、textField键盘遮挡
- iOS键盘的监听,以及处理遮挡问题和管理问题
- iOS防止键盘的遮挡
- ios 收起键盘
- iOS 键盘收起~
- Android面试经典题
- 导出product 产品,启动时会出现cmd界面
- 被Python等等乱码问题折腾的死去活来,看了几篇好文章
- JAVA方法 字符串与unicode的相互转换
- 实现QT元类型和QT线程通信
- iOS TextFiled、TextView 关于键盘的收起以及处理键盘遮挡
- 如何避免图片加载OOM
- 用VLC做流媒体服务器
- With the approaching end of FIFA 15, FIFA16 is upcoming now
- 正则匹配所有
- 判断两张图片相点击图片取色值
- 什么是性能瓶颈
- synchronized的使用规则
- Ubuntu14.04+Python3.4+apache2.4+Django1.7站点发布笔记