標籤:
參考自: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)的問題