标签:
参考自:http://blog.csdn.net/windkisshao/article/details/21398521
1.自定方法 ,用于移动视图
-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight withDuration:(NSTimeInterval)_NSTimeInterval;
2.注册监听
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[defaultCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
3.实现方法
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
if(nil==self.myTextView) return;// self.editTextView 为被键盘遮挡住的控件
CGRect rect = self.myTextView.frame;
float textY = rect.origin.y + rect.size.height;
float bottomY = SCREENHEIGHT - textY;//得到下边框到底部的距离 SCREENHEIGHT 为当前设备的高度
if(bottomY >=keyboardRect.size.height ){//键盘默认高度,如果大于此高度,则直接返回
return;
}
float moveY = keyboardRect.size.height - bottomY;
[self moveInputBarWithKeyboardHeight:moveY withDuration:animationDuration];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary* userInfo = [notification userInfo];
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration];
}
-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight withDuration:(NSTimeInterval)_NSTimeInterval{
CGRect rect1 = self.view.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:_NSTimeInterval];
rect1.origin.y = -_CGRectHeight;//view往上移动
self.view.frame = rect1;
[UIView commitAnimations];
}
测试的时候是用的4s真机测试的,一切都很完美,但是前几天同事用6 测的时候发现一个问题,就是编辑的时候能达到不遮挡控件的效果,但是发现输入中文的时候有时候会莫名的敲不出中文,会出现一连串的字符,而且删除的时候字符删不掉,几乎是越删越多.但是用4S测了很多遍都没有出现这个问题.这几天一直在想这个问题是什么造成的,望知道的高人们,指点一二.感谢!(用的UITextView)
IOS 关于键盘遮挡编辑区域(UITextFiled/UITextView)的问题
标签:
原文地址:http://www.cnblogs.com/Cyan-zoey/p/5133167.html