【问题标题】:How to make a UITextView scroll all the way to the bottom when the keyboard is visible当键盘可见时如何使 UITextView 一直滚动到底部
【发布时间】:2010-11-23 23:22:42
【问题描述】:

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

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

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

非常感谢大家

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitextview


    【解决方案1】:

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

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

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

    首先设置UITextField的delegate = 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];
        }
    

    【讨论】:

    • 我之前读过那个页面,但不确定它是否适用,因为他有一个带有一堆文本字段的滚动视图。我的笔尖只有一个 UITextView 作为主视图。再加上这些解决方案似乎过于复杂,我们这里是否缺少一些简单的设置?
    • 在您的情况下,为了确保它保持可见,您需要在键盘显示时调整文本字段的大小,并在隐藏时再次调整它的大小。查看我更新中的代码,看看是否对您有帮助。
    • @Chris : UITextView 是一个滚动视图,所以同样的技术也适用。没有简单的“按我的意思做魔术”设置。
    【解决方案2】:

    当编辑开始时,这会很好地将您的 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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-11
      • 2021-10-14
      • 1970-01-01
      • 2013-01-29
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多