当键盘存在时如何使UITextView向上移动

如何在开始input某个值时向上和向下移动UITextView 。 像在TextField中一样,我们使用它的委托方法。 在UITextView的情况下该怎么办?

编辑完整的视图将向上移动,完成编辑后将向下移动…

 - (void)textViewDidBeginEditing:(UITextView *)textView { [self animateTextView: YES]; } - (void)textViewDidEndEditing:(UITextView *)textView { [self animateTextView:NO]; } - (void) animateTextView:(BOOL) up { const int movementDistance =heightKeyboard; // tweak as needed const float movementDuration = 0.3f; // tweak as needed int movement= movement = (up ? -movementDistance : movementDistance); NSLog(@"%d",movement); [UIView beginAnimations: @"anim" context: nil]; [UIView setAnimationBeginsFromCurrentState: YES]; [UIView setAnimationDuration: movementDuration]; self.view.frame = CGRectOffset(self.inputView.frame, 0, movement); [UIView commitAnimations]; } 

我希望这能帮到您…

这里是一个示例代码,将自动为您处理键盘。 键盘避免

如果你正在使用TableView,那么你的tableView必须是TPKeyboardAvoidingTableView的子类,如果你使用的是scrollview,那么你的scrollview必须是TPKeyboardAvoidingScrollView的子类。 而这个库会自动为你做键盘处理。

我build议你看看这个指南

特别是在部分: 移动位于键盘下的内容 。 我多次成功地使用了这种方法。

我通过改变框架。

 -(void)textViewDidBeginEditing:(UITextView *)textView{ [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewMsg cache:YES]; viewMsg.frame = CGRectMake(10, 50, 300, 200); [UIView commitAnimations]; NSLog(@"Started editing target!"); } -(void)textViewDidEndEditing:(UITextView *)textView { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewMsg cache:YES]; viewMsg.frame = CGRectMake(10, 150, 300, 200); [UIView commitAnimations]; } 
 #define kOFFSET_FOR_KEYBOARD 80.0 -(void)keyboardWillShow { // Animate the current view out of the way if (self.view.frame.origin.y >= 0) { [self setViewMovedUp:YES]; } else if (self.view.frame.origin.y < 0) { [self setViewMovedUp:NO]; } } -(void)keyboardWillHide { if (self.view.frame.origin.y >= 0) { [self setViewMovedUp:YES]; } else if (self.view.frame.origin.y < 0) { [self setViewMovedUp:NO]; } } -(void)textFieldDidBeginEditing:(UITextField *)sender { if ([sender isEqual:mailTf]) { //move the main view, so that the keyboard does not hide it. if (self.view.frame.origin.y >= 0) { [self setViewMovedUp:YES]; } } } //method to move the view up/down whenever the keyboard is shown/dismissed -(void)setViewMovedUp:(BOOL)movedUp { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.3]; // if you want to slide up the view CGRect rect = self.view.frame; if (movedUp) { // 1. move the view's origin up so that the text field that will be hidden come above the keyboard // 2. increase the size of the view so that the area behind the keyboard is covered up. rect.origin.y -= kOFFSET_FOR_KEYBOARD; rect.size.height += kOFFSET_FOR_KEYBOARD; } else { // revert back to the normal state. rect.origin.y += kOFFSET_FOR_KEYBOARD; rect.size.height -= kOFFSET_FOR_KEYBOARD; } self.view.frame = rect; [UIView commitAnimations]; } - (void)viewWillAppear:(BOOL)animated { // register for keyboard notifications [[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 { // unregister for keyboard notifications while not visible. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } 

试试这个代码…..