简体   繁体   English

进入聊天屏幕时,如何让对话默认显示在底部,最近的消息显示在底部?

[英]How do i get conversation to display at the bottom by default where recent messages are present when entering chat screen?

After tapping on a conversation, the chat screen will start at the top and then scroll all the way to the bottom.点击对话后,聊天屏幕将从顶部开始,然后一直滚动到底部。 How can i bypass starting at the top and the scrolling to immediately displaying the most recent message at the bottom.我如何绕过从顶部开始并滚动到立即在底部显示最新消息。 This line of code is the closest thing i have found to doing what i want这行代码是我发现做我想做的最接近的事情

messagesCollectionView.scrollToLastItem(animated: true)

This is how it's currently displaying and then it will scroll to the bottom when i enter into the conversation (notice date)这是它当前的显示方式,然后当我进入对话时它会滚动到底部(通知日期)

在此处输入图像描述

This is how i want it do display upon entering the conversation (notice date)这就是我希望它在进入对话时显示的方式(通知日期)

这是

 private func listenForMessages(id: String) {
        Networking.shared.getComments(requestId: 15836, completionHandler: { [weak self] (result) in
            switch result {
            case .success(let comments):
                guard !comments.isEmpty else {
                    return
                }
                
                self?.messages = comments
                DispatchQueue.main.async {
                    self?.messagesCollectionView.reloadData()
                    self?.messagesCollectionView.scrollToLastItem()
                    
                } 
            case .failure(let error):
                print("failed to get messages: \(error)")
            }
    })
        }

You would probably just be able to say animated: false in that line.您可能只能在该行中说animated: false

You've not posted what you're doing in scrollToLastItem , you can try this though:你还没有在scrollToLastItem中发布你在做什么,你可以试试这个:

 let lastItemIndex = IndexPath(item: collectionArray.count - 1, section: 0)
            self.collectionView.scrollToItem(at: lastItemIndex, at: .bottom, animated: false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从coreData swift获取用户的最近聊天 - how can i get recent chat of a user from coreData swift 如何在 tableview 底部添加新行 - 聊天消息 - How to add new rows at bottom of tableview - chat messages 进入屏幕时如何自动播放音频 - How to automatically play audio when entering a screen 我如何使用自动布局将按钮放在屏幕底部 - how do i put button at bottom of screen using autolayout 如何使用WKWebView在屏幕底部停止空白? - How do I stop white space at bottom of screen with WKWebView? 当应用程序在 SWIFT 中进入前台时如何呈现 Lock_ViewController? - How to present Lock_ViewController when app is entering foreground in SWIFT? 如何绕过欢迎屏幕UI并在Firebase Auth中仅显示电话身份验证UI屏幕? - How do I bypass the welcome screen UI and present only the phone auth UI screen in Firebase Auth? 收到新消息时如何顺利更新聊天视图 - How to update chat view smoothly when I get new message 使用Storyboard Refrences时如何显示新的UIViewController - How do I present a new UIViewController when Storyboard Refrences are used 如何获取屏幕大小直到标签栏底部 - how to get the screen size until the bottom of the tabbar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM