xcode UICollectionView 顶部和侧面的浮动标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15159559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:53:01  来源:igfitidea点击:

UICollectionView Floating Headers on Top and Side

objective-cxcodeheaderuicollectionviewsupplementary

提问by Mason Wolters

How do you implement headers in a UICollectionView? I know you can put in supplementary views, but I don't know how to make them "float" above a section like headers in a UITableViewdo.

你如何在 a 中实现标题UICollectionView?我知道你可以添加补充视图,但我不知道如何让它们像标题一样“浮动”在一个部分之上UITableView

Here's my situation: I have a collectionViewwith the cells laid out in a grid format. You can scroll horizontally and vertically. I want to have a header on top so that when you scroll horizontally it scrolls horizontally with it, but it doesn't scroll vertically. I also want the same sort of thing on the left side where it scrolls vertically with the collectionViewbut not horizontally. Is implementing this with supplementary views the right approach?

这是我的情况:我有一个collectionView以网格格式布置的单元格。您可以水平和垂直滚动。我想在顶部有一个标题,这样当您水平滚动时,它会随之水平滚动,但不会垂直滚动。我也想在左侧使用相同的东西,它可以垂直滚动collectionView但不是水平滚动。用补充意见来实现这一点是正确的方法吗?

The final functionality I am looking for is similar to that of the Numbers spreadsheet app on iOS.

我正在寻找的最终功能类似于 iOS 上的 Numbers 电子表格应用程序。

Thanks in advance for your help!

在此先感谢您的帮助!

回答by AMayes

Update for iOS9:

iOS9 更新:

let flow = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
flow.sectionHeadersPinToVisibleBounds = true

Pass it along.

传过去。

回答by Audun Kjelstrup

EDIT: As mentioned in the comments, this answer is valid, but not for multiple sections. See this blog for a better solution: http://blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-using#notes

编辑:如评论中所述,此答案有效,但不适用于多个部分。请参阅此博客以获得更好的解决方案:http: //blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-using#notes



You need to specify that behaviour in your layout:

您需要在布局中指定该行为:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
  NSMutableArray* attributesArray = [[super layoutAttributesForElementsInRect:rect] mutableCopy];

  BOOL headerVisible = NO;

  for (UICollectionViewLayoutAttributes *attributes in attributesArray) {
    if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
      headerVisible = YES;
      attributes.frame = CGRectMake(self.collectionView.contentOffset.x, 0, self.headerReferenceSize.width, self.headerReferenceSize.height);
      attributes.alpha = HEADER_ALPHA;
      attributes.zIndex = 2;
    }
  }

  if (!headerVisible) {
    UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                                        atIndexPath:[NSIndexPath
                                                                                                     indexPathForItem:0
                                                                                                     inSection:0]];
    [attributesArray addObject:attributes];
  }

  return attributesArray;
}