Scroll UICollectionView to bottom

前端 未结 13 897
梦毁少年i
梦毁少年i 2021-02-02 12:05

I would like to scroll the UICollectionView to the bottom so the last item is in the view. I have tried to use scrollToItemAtIndexPath but it does not seem to be working. I want

13条回答
  •  醉梦人生
    2021-02-02 12:46

    I have added the lines below to run once the query is complete.

    var item = self.collectionView(self.collectionView!, numberOfItemsInSection: 0) - 1
    var lastItemIndex = NSIndexPath(forItem: item, inSection: 0)
    self.collectionView?.scrollToItemAtIndexPath(lastItemIndex, atScrollPosition: UICollectionViewScrollPosition.Top, animated: false)
    

    Update to Swift 5

    let item = self.collectionView(self.collectionView, numberOfItemsInSection: 0) - 1
    let lastItemIndex = IndexPath(item: item, section: 0)
    self.collectionView.scrollToItem(at: lastItemIndex, at: .top, animated: true)
    

提交回复
热议问题