xcode CollectionViewController scrollToItemAtIndexPath not working

前端 未结 9 956
故里飘歌
故里飘歌 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:51

    Sometimes collectionView(_:didSelectItemAt:) is either not called on the main thread, or blocks it, causing scrollToItem(at:at:animated:) to not do anything.

    Work around this by doing:

    DispatchQueue.main.async {
      collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
    }
    

提交回复
热议问题