代码之家  ›  专栏  ›  技术社区  ›  Chris

如何使UITextView在键盘可见时一直滚动到底部

  •  2
  • Chris  · 技术社区  · 14 年前

    我有一个可编辑的UITextView,里面有几页文字。当用户点击里面,它会打开键盘,它会隐藏文本的底部,你无法滚动查看它。

    有什么明显/简单/标准的方法来处理这个问题吗?我认为这是一个共同的问题。我想当键盘打开时,你必须调整文本视图的大小,或者类似的事情?

    另外,当他们点击页面下半部分的文本视图时,如何使其自动滚动,以便当键盘出现时,他们点击的行可见?或者,如果我在键盘出现时调整文本视图的大小,就会自动处理这个问题。

    多谢各位

    2 回复  |  直到 14 年前
        1
  •  4
  •   Community Fabien Hure    7 年前

    本文对此进行了广泛讨论: How to make a UITextField move up when keyboard is present?

    我个人过去用过Shiun的解决方案,效果很好。

    更新: 如果不想使用该方法,一个稍微简单的方法是在键盘显示时调整文本字段的大小。最好按照我在上面发布的链接上的说明操作,因为键盘将显示通知,让您可以访问键盘高度。

    首先设置UITextField=self的委托。然后:

    -(void)textFieldDidBeginEditing:(UITextField *)textField { // This is where the keyboard becomes visible
        textField.frame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, textField.frame.size.width, textField.frame.size.height-100);
    }
    
    -(void)textFieldDidEndEditing:(UITextField *)textField { // This is where the keyboard hides itself
        textField.frame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, textField.frame.size.width, textField.frame.size.height+100);
    }
    

    你可以根据你的方向等调整100。如果你想添加一些动画,你可以:

    -(void)textFieldDidBeginEditing:(UITextField *)textField { // This is where the keyboard becomes visible
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDuration:0.5];
            [UIView setAnimationBeginsFromCurrentState:YES];
            textField.frame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, textField.frame.size.width, textField.frame.size.height-100);
            [UIView commitAnimations];
        }
    
        -(void)textFieldDidEndEditing:(UITextField *)textField { // This is where the keyboard hides itself 
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDuration:0.5];
            [UIView setAnimationBeginsFromCurrentState:YES];
            textField.frame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, textField.frame.size.width, textField.frame.size.height+100);
            [UIView commitAnimations];
        }
    
        2
  •  1
  •   Karlis    11 年前

    编辑开始时,这将很好地将UITextView滚动到键盘顶部。如果您的UITextView具有动态高度(键入时自动显示/自动调整大小),这也将起作用。在iOS7中测试。

    调用键盘观察者方法并将UITextView委托设置为当前类:

    - (void)viewDidLoad
    {
        ...
        [self observeKeyboard];
        textView.delegate = (id)self;
    }
    

    为UIKeyboardDidShowNotification和UIKeyboardWillShowNotification添加键盘观察者:

    - (void)observeKeyboard
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    }
    

    获取键盘大小:

    - (void)keyboardWillShow:(NSNotification *)notification
    {
        CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        _keyboardHeight = keyboardSize.height;
    } 
    
    - (void)keyboardDidShow:(NSNotification *)notification
    {
        CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        _keyboardHeight = keyboardSize.height;
    }
    

    当UITextView开始编辑或值已更改时调用 scrollKeyboardToTextView :

    - (void)textViewDidBeginEditing:(UITextView *)textView
    {
        [self scrollKeyboardToTextView:textView];
    }
    
    - (void)textViewDidChange:(UITextView *)textView
    {
        [self scrollKeyboardToTextView:textView];
    }
    

    将带动画的UITextView滚动到键盘顶部:

    - (void)scrollKeyboardToTextView:(UITextView *)textView
    {
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, _keyboardHeight, 0.0);
        self.scrollView.contentInset = contentInsets;
        self.scrollView.scrollIndicatorInsets = contentInsets;
    
        CGRect aRect = self.view.frame;
        aRect.size.height -= _keyboardHeight;
        CGPoint origin = textView.frame.origin;
        origin.y -= self.scrollView.contentOffset.y;
        origin.y += textView.frame.size.height;
    
        CGPoint scrollPoint = CGPointMake(0.0, textView.frame.origin.y + textView.frame.size.height - (aRect.size.height));
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }