【问题标题】:How to scroll up textview automatically when new text entered like in chat apps?如何在聊天应用程序中输入新文本时自动向上滚动文本视图?
【发布时间】:2016-03-10 07:18:51
【问题描述】:

我正在制作一个聊天应用程序,我想这样做,当使用按钮发送文本时,textview 将向上滚动并显示文本。现在发生了什么,我必须在发送聊天时滚动视图而不是自动。 谢谢

-(void)scrollTextViewToBottom:(UITextView *)textView 
{ 
    if(_tvChat.text.length > 0 ) 
    { 
        NSRange bottom = NSMakeRange(textView.text.length -1, 2);     
        [_tvChat scrollRangeToVisible:bottom]; 
    } 
}

这正在发生 当点击发送按钮和文字时我想要这样的东西会显示

【问题讨论】:

  • 你试过设置 contentOffset 吗?此外,发送消息后,您应该清空 UITextView 对象。
  • -(void)scrollTextViewToBottom:(UITextView *)textView { if(_tvChat.text.length > 0 ) { NSRange bottom = NSMakeRange(textView.text.length -1, 2); [_tvChat scrollRangeToVisible:bottom]; } } 我已经这样做了
  • 如果您在聊天应用程序中使用 tableview,那么它会比 UITextView 好得多,并且使用 tableview 委托可以轻松解决滚动问题。
  • @Gagan_iOS 如果您有任何 textview 自动滚动的代码,请发送....
  • Aayush,我没有任何 textview 代码,但是是的,我有一个 tableview 示例。 github.com/sunillohar/quickblox/tree/master/sample-chat 下载此代码和视图文件夹找到类 ChatMessageTableViewCell。将此添加到您的项目中并按照您在下载的代码中找到的执行设置。它会完美运行。

标签: ios scroll uitextview


【解决方案1】:

你必须实现一个委托方法。下面的代码说。如果你点击返回。滚动到 textView 底部

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

    if ([text isEqualToString:@"\n"]) {
        textView.contentOffset = CGPointMake(0.0, textView.contentSize.height);
    }
    return YES;
}

【讨论】:

    【解决方案2】:

    试试这个

    CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
    [self.scrollView setContentOffset:bottomOffset animated:YES];
    

    【讨论】:

      猜你喜欢
      • 2012-05-06
      • 2022-01-16
      • 2023-04-08
      • 2015-10-26
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      相关资源
      最近更新 更多