は、viewWillAppear
で動作します。私は3つの画面を追加しました.1stはきれいで、2番目はcollectionViewを含み、最後は空です。第2のスクリーンの
実装(viewWillAppearはUIScrollViewの拡張メソッドとスクロールコンテンツをトリガーする)以下である:
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
collectionView.scrollToBottom(animated: false)
}
}
extension ViewController: UICollectionViewDelegate,
UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 200
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
return cell
}
}
ここでコンテンツをスクロールするためextension
です:
extension UIScrollView {
func scrollToTop(animated: Bool) {
setContentOffset(CGPoint(x: 0, y: -contentInset.top),
animated: animated)
}
func scrollToBottom(animated: Bool) {
setContentOffset(CGPoint(x: 0, y: CGFloat.greatestFiniteMagnitude),
animated: animated)
}
}
はnew clean
プロジェクトを作ってみると、それで遊びます。
'animated'を' false'に設定しようとしましたか? – JuicyFruit
@JuicyFruit、はい、それは視覚的に悪化しています。 – danilabagroff
'collection view'を非表示にし、' alpha'を0に設定し、 'scrolltoitem'の後に' alpha 'をアニメーション表示します。 – JuicyFruit