【问题标题】:How to load collection view at bottom of the screen?如何在屏幕底部加载集合视图?
【发布时间】:2020-07-29 05:45:21
【问题描述】:

我有一个显示消息的集合视图。现在它加载消息然后滚动到底部

let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
self.collectionView?.scrollToItem(at: indexPath, at: .bottom, animated: true)

这可行,但有大量消息和 collectionview 项目,它滚动非常缓慢并且非常不稳定。 我想要发生的是集合视图从底部开始,并且不必滚动浏览所有消息并且看起来就像在消息上所做的那样。这个我也试过了,还是不行

let point = CGPoint(x: 0, y: self.collectionView.frame.size.height)
self.collectionView.setContentOffset(point, animated: false)

【问题讨论】:

    标签: ios swift xcode


    【解决方案1】:

    如果您不想滚动消息并立即到达底部,请将animated 参数设置为false

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

    【讨论】:

      【解决方案2】:

      您可以在viewDidLoad 底部尝试此代码,它在 iOS 13 中运行良好,我不在其他版本中测试:

      DispatchQueue.main.async {
          let lastIndexPath = IndexPath(item: self.messages.count - 1, section: 0)
          self.collectionView.scrollToItem(at: lastIndexPath, at: .centeredVertically, animated: false)
      }
      

      如果您将第二个代码放入DispatchQueue.main.async,它应该可以工作,(我认为...)

      【讨论】:

        猜你喜欢
        • 2019-05-02
        • 2011-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多