UICollectionView automatically scroll to bottom when screen loads

后端 未结 9 2098
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条回答
  • 2021-02-05 02:50

    For me, i found next solution:

    call reloadData in CollectionView, and make dcg on main to scroll.

     __weak typeof(self) wSelf = self;
    
     [wSelf.cv reloadData];
     dispatch_async(dispatch_get_main_queue(), ^{
     NSLog(@"HeightGCD:%@", @(wSelf.cv.contentSize.height));
     [wSelf.cv scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:50 inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
     });
    
    0 讨论(0)
  • 2021-02-05 02:54

    Swift 3 example

    let sectionNumber = 0
    self.collectionView?.scrollToItem(at: //scroll collection view to indexpath
                                      NSIndexPath.init(row:(self.collectionView?.numberOfItems(inSection: sectionNumber))!-1, //get last item of self collectionview (number of items -1)
                                                       section: sectionNumber) as IndexPath //scroll to bottom of current section
                                    , at: UICollectionViewScrollPosition.bottom, //right, left, top, bottom, centeredHorizontally, centeredVertically
                                    animated: true)
    
    0 讨论(0)
  • 2021-02-05 02:54

    The issue is likely that even if your collection view is on screen, it might not have the actual contentSize.

    If you scroll in viewDidAppear, you will have a contentSize, but your scollectionview will briefly show content before scrolling.

    And the problem with viewDidLayoutSubviews is that it is called multiple times, so you then need to add an ugly boolean to limit scrolling.

    The best solution i've found is to force layout in view will appear.

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // force layout before scrolling to most recent
        collectionView.layoutIfNeeded()
        // now you can scroll however you want
        // e.g. scroll to the right
        let offset = collectionView.contentSize.width - collectionView.bounds.size.width
        collectionView.setContentOffSet(CGPoint(x: offset, y: 0), animated: animated)
    }
    
    0 讨论(0)
  • 2021-02-05 02:55

    none of these were working so well for me, I ended up with this which will work on any scroll view

    extension UIScrollView {
        func scrollToBottom(animated: Bool) {
            let y = contentSize.height - 1
            let rect = CGRect(x: 0, y: y + safeAreaInsets.bottom, width: 1, height: 1)
            scrollRectToVisible(rect, animated: animated)
        }
    }
    
    0 讨论(0)
  • 2021-02-05 02:57

    Get indexpath for last item. Then...

    - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated
    
    0 讨论(0)
  • 2021-02-05 03:00

    I found that nothing would work in viewWillAppear. I can only get it to work in viewDidLayoutSubviews:

    - (void)viewDidLayoutSubviews
    {
        [super viewDidLayoutSubviews];
    
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:endOfModel inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
    
    0 讨论(0)
提交回复
热议问题