UICollectionView automatically scroll to bottom when screen loads

后端 未结 9 2095
Happy的楠姐
Happy的楠姐 2021-02-05 02:44

I\'m trying to figure out how to scroll all the way to the bottom of a UICollectionView when the screen first loads. I\'m able to scroll to the bottom when the status bar is tou

9条回答
  •  梦毁少年i
    2021-02-05 03:09

    So had a similar issue and here is another way to come at it without using scrollToItemAtIndexPath

    This will scroll to the bottom only if the content is larger than the view frame.

    It's probably better to use scrollToItemAtIndexPath but this is just another way to do it.

    CGFloat collectionViewContentHeight = myCollectionView.contentSize.height;
    CGFloat collectionViewFrameHeightAfterInserts = myCollectionView.frame.size.height - (myCollectionView.contentInset.top + myCollectionView.contentInset.bottom);
    
    if(collectionViewContentHeight > collectionViewFrameHeightAfterInserts) {
       [myCollectionView setContentOffset:CGPointMake(0, myCollectionView.contentSize.height - myCollectionView.frame.size.height) animated:NO];
    }
    

提交回复
热议问题