2017-03-19 11 views
1

多くのソリューションを試しましたが、どれも機能しませんでした。質問はシンプルです - viewDidLoadviewWillAppearまたはviewDidAppearの間にスクロールする方法は、視覚的(ジャンプ)効果なしでですか?私が今持っている何コレクションビューの最初のスクロール位置を設定します

テストプロジェクトにチェック
public override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 
    if (!self.layoutFlag) { 

     if let lastCell: IndexPath = self.lastCellIndexPath { 
      self.collectionView?.scrollToItem(at: lastCell, at: UICollectionViewScrollPosition.bottom, animated: true) 
      self.layoutFlag = true 
     } 
    } 
} 
+1

'animated'を' false'に設定しようとしましたか? – JuicyFruit

+0

@JuicyFruit、はい、それは視覚的に悪化しています。 – danilabagroff

+0

'collection view'を非表示にし、' alpha'を0に設定し、 'scrolltoitem'の後に' alpha 'をアニメーション表示します。 – JuicyFruit

答えて

0

は、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プロジェクトを作ってみると、それで遊びます。

+0

これは単に遊び場で動作する場合、どのように役立ちますか? – danilabagroff

+1

@danilabagroffまあ、テストプロジェクト(その遊び場ではない)で動作する場合は、すでにうまくいくはずの解決策があります。元のプロジェクトからいくつかの機能をゆっくりと除外し、グリッチの原因を調査し、修正方法を見つけることができます。 –

0

viewDidAppear(_:)メソッドにインデックスパス呼び出しにスクロールを追加し、falseに設定animatedパラメータがありますので、ビューがユーザーに表示される前に、コレクションビューを下にスクロールしなければならない

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 

    if !self.layoutFlag { 
     if let lastCell: IndexPath = self.lastCellIndexPath { 
      self.collectionView?.scrollToItem(at: lastCell, at: UICollectionViewScrollPosition.bottom, animated: false) 
      self.layoutFlag = true 
     } 
    } 
} 

を「ジャンプ」しない。

関連する問題