Swift NSCollection从下至上查看流量



我正在为macOS开发一个用Swift编写的消息客户端。我使用带有NSCollectionView的NSCrollView作为documentView来呈现消息。目前,我已经实现了无限滚动,但现在的问题是collectionView从顶部开始加载单元格,然后向下滚动——这是NSCollectionView的默认行为。相反,我需要它从底部开始,然后向上工作——就像典型的消息应用程序显示消息的方式一样。

我尝试过的解决方案:

  • 一旦加载视图或用户选择不同的对话,就滚动到集合视图的底部。这个解决方案明显很刺耳,真的把无限滚动搞砸了
  • 覆盖scrollView、scrollView的contentView和collectionView中的isFlipped变量。这样做对任何视图都没有明显的影响
  • 按π弧度旋转整个滚动视图、collectionView、contentView或collectionView单元格。作为一种绝望的措施,我试图旋转整个滚动视图,但无法做到这一点,也无法旋转任何collectionView项目。我做了一些类似wantsLayer = true layer!.setAffineTransform(CGAffineTransform(rotationAngle: .pi)) updateLayer() setNeedsDisplay(frameRect)的事情。这同样没有明显的影响

让NSCollectionView自下而上的最佳方式是什么?顺便说一句,我没有使用情节提要。

嗨,我不确定这段代码是否会对你有所帮助,因为我已经将它用于iPhone聊天应用程序,它对我很有效。在我的情况下,我只是将collectionView放在视图控制器中。

//MARK:- View Controller life cycle method
override func viewDidLoad() {
chatCollectionView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if(keyPath == "contentSize"){
if let newvalue = change?[.newKey] {
let contentHeight: CGFloat = chatCollectionView.contentSize.height
let collectionHeight = chatCollectionView.frame.size.height
if contentHeight < collectionHeight {
var insets: UIEdgeInsets = chatCollectionView.contentInset
insets.top = collectionHeight - contentHeight
chatCollectionView.contentInset = insets
} else {
chatCollectionView.contentInset = .zero
}
}
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
chatCollectionView.removeObserver(self, forKeyPath: "contentSize")
}

最新更新