【问题标题】:Easy way to reset scroll view position [duplicate]重置滚动视图位置的简单方法[重复]
【发布时间】:2017-08-21 10:00:45
【问题描述】:

我有水平 UICollectionView,我想将它重置为 X 位置。我尝试了以下操作:

 let indexPath = IndexPath(row: 0, section: 1)

 self.collectionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition(rawValue: 0), animated: false)

然而它不起作用,我得到一个错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path: <NSIndexPath: 0x61000002d040> {length = 2, path = 1 - 0}'

如何滚动集合视图开始?

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

    如果没有项目,这将不起作用

     self.collectionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition(rawValue: 0), animated: false)
    

    使用(目标 C):

    CGPoint topOffest = CGPointMake(0,-self.collectionView.contentInset.top);
    [self.collectionView setContentOffset:topOffest animated:YES];
    

    使用(斯威夫特):

    let topOffest:CGPoint = CGPoint(0,-self.collectionView.contentInset.top)
    collectionView?.setContentOffset(topOffest, animated: true)
    

    【讨论】:

    • 谢谢,但我喜欢用 Swift 表示答案
    • 让 topOffest:CGPoint = CGPoint(0,-self.collectionView.contentInset.top) collectionView?.setContentOffset(topOffest, animated: true)
    【解决方案2】:
    self.collectionView.scrollToItem(at: [0,5], at:.centeredHorizantally, animated: false)
    

    0 代表第 0 段编号

    5 代表 5 号单元格

    【讨论】:

    • self.collectionView.scrollToItem(at:IndexPath(item: index, section: 0), at: .centeredHorizantally, animated: false)
    猜你喜欢
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    相关资源
    最近更新 更多