Ios UICollectionViewCell覆盖粘性标题
我对Ios UICollectionViewCell覆盖粘性标题,ios,uicollectionview,uicollectionviewcell,Ios,Uicollectionview,Uicollectionviewcell,我对UICollectionView有一种奇怪的行为 在图1中,显示了当用户向上滚动集合时的正确行为:项目位于header视图下 [图1] 当我在搜索后重新加载项目时,一些项目会在滚动期间越过标题 我不知道原因。。。 有什么想法或建议吗? 谢谢 编辑 您可以尝试在UICollectionViewFlowLayout上设置sectionHeadersPinToVisibleBounds=true属性。因为每个UICollectionView都有自己的布局 一个布尔值,指示在滚动期间标题是否固
UICollectionView
有一种奇怪的行为
在图1中,显示了当用户向上滚动集合时的正确行为:项目位于header视图下
[图1]
当我在搜索后重新加载项目时,一些项目会在滚动期间越过标题
我不知道原因。。。
有什么想法或建议吗?
谢谢
编辑
您可以尝试在UICollectionViewFlowLayout上设置sectionHeadersPinToVisibleBounds=true属性。因为每个UICollectionView都有自己的布局 一个布尔值,指示在滚动期间标题是否固定到集合视图边界的顶部 也许会有帮助。将此代码添加到viewDidLoad中。
(self.yourCollectionView.collectionViewLayout as! UICollectionViewFlowLayout).sectionHeadersPinToVisibleBounds = true
UICollectionView和UIViewController内的HeaderView的工作代码:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor blueColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(100, 100);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
return view;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(self.view.frame.size.width, 100);
}
情节提要:
输出:
请检查这个。希望这能在其他方面对您有所帮助。对我有效的方法是调用
reloadData()
,而不是尝试用动画重新加载(reloadSections()
)
在我的例子中,我有一个show/hide按钮来在item count和0之间切换节中的行数(展开节的内容) 当我替换
collectionView?.reloadSections([section])
与
我不再有这个问题。在我的案例中尝试了许多有效的解决方案后
collectionView.clipsToBounds = true
您使用的是UIViewController还是UICollectionViewController?能否告诉我UICollectionView的顶部约束在哪里?这是在UIView顶部布局上还是在搜索栏底部?@ABLASHBNAIR UIViewController上添加了UICollectionViewit@AbilashBNair搜索栏位于标题中;视图控制器中没有添加外部元素吗?是否设置标题的高度?请为代码添加一些说明,以使其更有用。我添加了一些说明。:)
collectionView?.reloadData()
collectionView.clipsToBounds = true