scrollToItem at indexPath at .top hides cell under header when sectionHeadersPinToVisibleBounds

天大地大妈咪最大 提交于 2019-12-23 13:11:45

问题


Using the following configuration:

let layout = UICollectionViewFlowLayout()
layout.sectionHeadersPinToVisibleBounds = true

let collectionViewController = UICollectionViewController(view.bounds, collectionViewLayout: layout)

The following code will scroll to the given index path but the item will be under and covered by its header:

let indexPath = IndexPath(section: 0, row: 2)
collecitonView.scrollToItem(at: indexPath, at: .top, animated: true)

How do I get the collection view to scroll to the item at indexPath without the item being covered by its section header when sectionHeadersPinToVisibleBounds is set to true?


回答1:


It seems like a iOS framework's bug. I'm using this way.

 guard let layout = collectionView.collectionViewLayout.layoutAttributesForItem(at: indexPath) else {
    collectionView.scrollToItem(at: indexPath, at: .top, animated: true)
    return
} 
let offset = CGPoint(x: 0, y: layout.frame.minY - headerHeight)
collectionView.setContentOffset(offset, animated: true)


来源:https://stackoverflow.com/questions/49638855/scrolltoitem-at-indexpath-at-top-hides-cell-under-header-when-sectionheaderspin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!