iOS--UITextView 与 键盘处理

2018-01-11 13:03:31来源:oschina作者:指尖流年人点击

分享

1.遵守协议。UITextViewDelegate


2.调用方法,点击空白区域,收回键盘


#pragma mark - 点击空白区域,收回键盘
- (void)setBackKeyboard {
NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
UITapGestureRecognizer *singleTapGR =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackKeyboard:)];
NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
[notiCenter addObserverForName:UIKeyboardWillShowNotification
object:nil
queue:mainQuene
usingBlock:^(NSNotification *note){[self addGestureRecognizer:singleTapGR];
}];
[notiCenter addObserverForName:UIKeyboardWillHideNotification
object:nil
queue:mainQuene
usingBlock:^(NSNotification *note){[self removeGestureRecognizer:singleTapGR];
}];
}
- (void)tapBackKeyboard:(UIGestureRecognizer *)tap {
//此method会将self.view里所有的subview的first responder都resign掉
[self endEditing:YES];
}

3.点击return 收回键盘


#pragma mark UITextViewDelegate 点击return收回键盘
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{if ([text isEqualToString:@"/n"]) {



[textView resignFirstResponder];

return NO;
}return YES;}

4.处理键盘的遮挡


#pragma mark UITextViewDelegate--开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if (iPhone5) {
CGFloat offset_y = 0.f;
if (textView.tag == 1000) {//获取到textView
offset_y = 200.f;//偏移的位置
}
CGPoint point = self.tableView.contentOffset;
point = CGPointMake(point.x, offset_y);
[UIView animateWithDuration:0.25 animations:^{
self.tableView.contentOffset = point;
}];
}
return YES;
}
#pragma mark UITextViewDelegate--结束编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
if (iPhone5) {
CGFloat offset_y = 0.f;
if (textView.tag == 1000) {
offset_y = 200.f;
}
CGPoint point = self.tableView.contentOffset;
point = CGPointMake(point.x, 0);
[UIView animateWithDuration:0.25 animations:^{
self.tableView.contentOffset = point;
}];
}
return YES;
}

5.处理通知


-(void)dealloc
{

[[NSNotificationCenter defaultCenter]removeObserver:self];}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台