問題は、コレクションビューのcontentOffsetをviewWillAppearに設定しようとすると、コレクションビューがそのアイテムをまだレンダリングしていないことです。したがって、self.collectionView.contentSizeはまだ{0,0}です。解決策は、コレクションビューのレイアウトにコンテンツサイズを尋ねることです。
また、contentSizeがコレクションビューの境界よりも大きい場合は、contentOffsetを設定する必要があります。
func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let contentSize: CGSize? = collectionView?.collectionViewLayout.collectionViewContentSize
if contentSize?.height > collectionView?.bounds.size.height {
let targetContentOffset = CGPoint(x: 0.0, y: (contentSize?.height)! - (collectionView?.bounds.size.height)!)
collectionView?.contentOffset = targetContentOffset
}
}