UICollectionView contentOffset随自定义布局而变化 [英] UICollectionView contentOffset changes with custom layout

查看:599
本文介绍了UICollectionView contentOffset随自定义布局而变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义UICollectionViewLayout的UICollectionView(实际上,我正在使用非常相似,但那里的解决方案对我不起作用。



我尝试在布局实现中修改 collectionViewContentSize ,以确保它总是大于collectionView的大小。虽然这意味着我可以向下滚动我的内容(它比collectionView的高度更短)并隐藏额外的空间,我也可以向上滚动查看它。



似乎没有任何作用!

解决方案

我能做的唯一解决方案得出那个几乎不可接受的行为:

   - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if(self.collectionView.contentOffset.y< 0){
self.collectionView.contentOffset = CGPointMake(self.collectionView.contentOffset.x,0.0);
}
}

除了将内容的高度设置为 fmax(self.collectionView.frame.size.height + 20,[self stackedSectionHeight]) in collectionViewContentSize



这会删除部分标题上方的空格,但会从顶部删除反弹。一个非常次优的解决方案,但相当可接受。



如果有人有一个,我会接受更好的答案,或者如果我找到一个,我会更新这个答案。


I have a UICollectionView with a custom UICollectionViewLayout (actually, I'm using this nice layout).

I set contentOffset = CGPointZero in viewDidLoad. After viewDidLoad, however, the offset is -20, and the content gets pushed down like so:


(It should be flush with the line). I'm loading the collection view layout in interface builder. It seems that my problem is very similar to this one, however the solutions there don't work for me.

I tried modifying collectionViewContentSize in my layout implementation to ensure it was always greater than the size of the collectionView. Although this means I can scroll my content down (it's shorter than the height of the collectionView) and hide the extra space, I can also scroll back up to see it.

Nothing seems to work!

解决方案

The only solution I could come up with that had barely-acceptable behavior:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.collectionView.contentOffset.y < 0) {
        self.collectionView.contentOffset = CGPointMake(self.collectionView.contentOffset.x, 0.0);
    }
}

As well as setting the height of the content to fmax(self.collectionView.frame.size.height + 20, [self stackedSectionHeight]) in collectionViewContentSize

This removes the space above the section header, but it removes the "bounce" from the top. A pretty sub-optimal solution, but fairly acceptable.

I'll accept a better answer if anyone has one, or if I find one I'll update this answer.

这篇关于UICollectionView contentOffset随自定义布局而变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆