当键盘出现时如何使UITextView上移

25

当您开始输入某些值时,如何将 UITextView 向上或向下移动?就像在TextField中我们使用其委托方法一样。那么在 UITextView 的情况下应该怎么办呢?


textFieldDidBecomeActive > 根据需要移动文本框。 - user2638922
请查看此链接:https://dev59.com/RHNA5IYBdhLWcg3wAI7e - BhavikKama
2
搜索难用吗?即使是这样,看看侧边栏:它会显示相关的问题。 - Abizern
5个回答

21

编辑时,完整视图会向上移动,编辑完成后会向下移动...

- (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];
    }

我希望这能对你有所帮助...


2
嗯,你应该使用块来进行动画,因为自iOS4以来,你的方法已经不被推荐使用了。 - Beuj
1
我在这段代码中遇到了很多问题。通过将“self.inputView.frame”替换为“self.view.frame”来解决了它。 - Rajesh
当您无法使用带有滚动视图的解决方案时,这是最简单和最好的方法。 - Alex Sorokoletov
你是如何获取键盘高度的? - Kishor Pahalwani

5
这里有一段示例代码,可以自动处理键盘。如果您正在使用TableView,则您的tableView必须是TPKeyboardAvoidingTableView的子类;如果您正在使用ScrollView,则您的scrollView必须是TPKeyboardAvoidingScrollView的子类。这个库会自动为您处理键盘。Keyboard avoiding

5
我建议您查看这个指南,特别是第移动位于键盘下方的内容节。我已经成功地使用了这种方法多次。

3

我通过更改框架来实现。

-(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];
}

2
#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];
}

试一下这段代码......


已经有一个关于同样问题的提问了。 - BhavikKama
2
你应该使用块来进行动画,因为自iOS4以来,你的方法已经被弃用了... - Beuj
这是针对textField的答案,不是textView。 - Shmidt
不应该使用键盘大小的常量,建议从苹果获取偏移大小。 - Matt Parkins

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接