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

时间:2010-11-23 23:22:42

标签: iphone objective-c cocoa-touch uitextview

我有一个可编辑的UITextView,其中包含几页文本。当用户点击它并调出键盘时,它会隐藏文本的底部,你无法滚动查看它。

是否有一些明显/简单/标准的方法来解决这个问题?我认为这是一个常见问题。我假设您必须在键盘启动时调整文本视图的大小,或类似的东西?

此外,当他们点击页面下半部分的文本视图时,如何使其自动滚动,以便在键盘出现时可以看到他们点击的线条?或者如果我在键盘出现时调整文本视图的大小,这将自动处理。

非常感谢你们

2 个答案:

答案 0 :(得分:4)

这里已经广泛讨论过:How to make a UITextField move up when keyboard is present?

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

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

首先设置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];
    }

答案 1 :(得分:1)

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

调用键盘观察者方法并将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];
}