今天开发中遇到一个小问题:
需求:实现支付宝的应用管理页面,点击标签collectionView滚动到指定的section.
![](https://img.haomeiwen.com/i1718697/11574a619a0d002d.png)
![](https://img.haomeiwen.com/i1718697/aac18e546e191083.png)
开始通过调用系统api:
![](https://img.haomeiwen.com/i1718697/17dc1f3f933a7aae.png)
但是这样头部显示不了,为了需求只能修改;在上面的代码之后设置contentOffset,但是会有一个下移动的效果
![](https://img.haomeiwen.com/i1718697/2e457e79a63c765b.png)
为了效果更加平滑,首先根据indexPath获取到headerview,然后设置contentOffset
![](https://img.haomeiwen.com/i1718697/4a99f958b11d5594.png)
然而由于indexPath超过可见视图范围,这个时候取到的headerview为nil.无法实现效果。不过通过oc另外一个方法可以实现效果
[self.collectionView layoutIfNeeded];
UICollectionViewLayoutAttributes *attributes =[self.collectionView layoutAttributesForItemAtIndexPath:indexPath];
CGRect rect = attributes.frame;
CGPoint point = CGPointMake(0, rect.origin.y - Get375Width(40));
[_collectionView setContentOffset:point animated:YES];
网友评论