xcode CollectionViewController scrollToItemAtIndexPath not working

前端 未结 9 909
故里飘歌
故里飘歌 2021-02-01 03:06

I have created a CollectionView Control and filled it with images. Now I want to scroll to item at a particular index on start. I have tried out scrollToItemA

9条回答
  •  孤城傲影
    2021-02-01 03:28

    Adding the scrolling logic to viewDidAppear worked for me:

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.collectionView?.scrollToItemAtIndexPath(
            someIndexPath,
            atScrollPosition: UICollectionViewScrollPosition.None,
            animated: animated)
    }
    

    Adding it to viewDidLoad doesn't work: it gets ignored.

    Adding it to viewDidLayoutSubviews doesn't work unless you want to scroll logic calling any time anything changes. In my case, it prevented the user from manually scrolling the item

提交回复
热议问题