2012-11-23 39 views
28

如何在UICollectionView的顶部添加标题视图/顶视图(而不是标题标题)?如何在UITollectionView中添加HeaderView像UITableView的tableHeaderView

它应该作为UITableViewtableHeaderView属性excactly。

因此,它需要位于第一部分标题视图顶部(位于索引0之前的部分之前),与其余内容一起滚动并进行用户交互。

我来了这么远,最好是做一个特殊的厦门国际银行(带MyCollectionReusableView子类的UICollectionReusableView作为文件的所有者),因为这是大到足以也包含在头我的子视图第一节头部视图,它是我认为这是一种黑客攻击,而且我还没有设法检测到触摸。

不知道我是否可以让我的MyCollectionReusableView子类允许接触或有更好的方法。

回答

5

我已经通过添加一个UIView作为集合视图的子视图来将视图放在集合视图的顶部。它向上滚动集合视图,它具有普通的UIView用户交互。如果您没有部分标题,这可以正常工作,但如果您没有部分标题,则不起作用。在这种情况下,我看不出你做这件事的方式有什么问题。我不确定你为什么没有检测到触摸,他们为我工作的很好。

+0

谢谢,我已经使用节标题,所以第一个(在索引0的节标题)我已经做得更大以包含标准节标题和集合视图顶部的视图。但是你说你在'MyCollectionReusableView'子类中触及了吗? – ChrHansen

+0

刚回去再试一次,你是对的 - 它的工作原理!我一直工作得太晚,我用'UIImageView'来检测触摸,并忘记在Xcode中为我的图像视图勾选“用户交互已启用”。 – ChrHansen

51
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height) collectionViewLayout:flowlayout]; 
self.collectionView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0); 
UIImageView *imagev = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"015.png"]]; 
imagev.frame = CGRectMake(0, -50, 320, 50); 
[self.collectionView addSubview: imagev]; 
[self.view addSubview: _collectionView]; 

我使用属性contentInset插入一个帧到UICollectionView的顶部,然后我的图像视图添加到它,并且它成功。我认为它可以很好地作为UITableViewtableHeaderView财产。你怎么看?

+0

我正在寻找这个,它比制作第一个部分头部更好。 –

+1

谢谢你。 –

+0

我已经使用上面的方式添加标题vie,无法获得标题视图上的触摸事件,任何想法为什么? –

2

达到此目的的最佳方法是为第一部分制作节标题。然后设置UICollectionViewFlowLayout的财产:

@property(nonatomic) BOOL sectionHeadersPinToVisibleBounds 

或迅速

var sectionHeadersPinToVisibleBounds: Bool 

为NO(false为SWIFT)。这将确保节标题与单元格连续滚动,并且不会像节标题通常那样固定到顶部。

+0

这对我有用,但是如果我想让一些sectionHeader固定在顶部,但是有些不是?这个UICollectionViewFlowLayout的属性喜欢全局集合。 – Neko

-2
private var PreviousInsetKey: Void? 
extension UIView { 

var previousInset:CGFloat { 
    get { 
     return objc_getAssociatedObject(self, &PreviousInsetKey) as? CGFloat ?? 0.0 
    } 
    set { 
     if newValue == -1{ 
      objc_setAssociatedObject(self, &PreviousInsetKey, nil, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 
     } 
     else{ 
      objc_setAssociatedObject(self, &PreviousInsetKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 
     } 
    } 
} 


func addOnCollectionView(cv: UICollectionView){ 
    if self.superview == nil{ 
     var frame = self.frame 
     frame.size.width = SCREEN_WIDTH 
     cv.frame = frame 
     self.addOnView(view: cv) 
     let flow = cv.collectionViewLayout as! UICollectionViewFlowLayout 
     var inset = flow.sectionInset 
     previousInset = inset.top 
     inset.top = frame.size.height 
     flow.sectionInset = inset 
    } 

} 

func removeFromCollectionView(cv: UICollectionView){ 
    if self.superview == nil{ 
     return 
    } 
    self.removeFromSuperview() 
    let flow = cv.collectionViewLayout as! UICollectionViewFlowLayout 
    var inset = flow.sectionInset 
    inset.top = previousInset 
    flow.sectionInset = inset 
    previousInset = -1 
} 

func addOnView(view: UIView){ 

    view.addSubview(self) 

    self.translatesAutoresizingMaskIntoConstraints = false 
    let leftConstraint = NSLayoutConstraint(item: self, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1.0, constant: 0) 
    let widthConstraint = NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: view.frame.size.width) 
    let heightConstraint = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: view.frame.size.height) 
    let topConstraint = NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: 0) 

    view.addConstraints([leftConstraint, widthConstraint, heightConstraint, topConstraint]) 
    self.layoutIfNeeded() 
    view.layoutIfNeeded() 
} 
} 
+1

你能解释一下你的解决方案在做什么? –

0

我想做到这一点它的子类UICollectionViewLayout(UICollectionViewFlowLayout),并添加所需的页眉或页脚属性的最佳方式。

由于所有项目包括根据其collectionViewLayout属性的UICollectionView布局中的补充视图,决定是否存在页眉(页脚)的是collectionviewlayout

使用contentInset比较容易,但是当您想要动态地隐藏或显示标题时可能会出现问题。

我已经写了一个协议来完成这个任务,UICollectionViewLayout的任何子类都可以采用这个协议来使它具有页眉或页脚。你可以找到它here.

主要的想法是创造的头一个UICollectionViewLayoutAttributeslayoutAttributesForElements(in rect: CGRect)返回它如果需要的话,你将不得不修改所有其他属性,所有它们的位置应该由头部的高度向下移动,因为标题在他们之上。

0

我结束了使用UICollectionView扩展添加我的自定义标题。使用特定的标签,我可以伪装存储的属性来识别我的自定义标题(从外部透明)。 如果在添加自定义标题时未完成UICollectionView的布局,则可能必须滚动到特定偏移量。

extension UICollectionView { 
    var currentCustomHeaderView: UIView? { 
     return self.viewWithTag(CustomCollectionViewHeaderTag) 
    } 

    func asssignCustomHeaderView(headerView: UIView, sideMarginInsets: CGFloat = 0) { 
     guard self.viewWithTag(CustomCollectionViewHeaderTag) == nil else { 
      return 
     } 
     let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height 
     headerView.frame = CGRect(x: sideMarginInsets, y: -height + self.contentInset.top, width: self.frame.width - (2 * sideMarginInsets), height: height) 
     headerView.tag = CustomCollectionViewHeaderTag 
     self.addSubview(headerView) 
     self.contentInset = UIEdgeInsets(top: height, left: self.contentInset.left, bottom: self.contentInset.bottom, right: self.contentInset.right) 
    } 

    func removeCustomHeaderView() { 
     if let customHeaderView = self.viewWithTag(CustomCollectionViewHeaderTag) { 
      let headerHeight = customHeaderView.frame.height 
      customHeaderView.removeFromSuperview() 
      self.contentInset = UIEdgeInsets(top: self.contentInset.top - headerHeight, left: self.contentInset.left, bottom: self.contentInset.bottom, right: self.contentInset.right) 
     } 
    } 
} 

CustomCollectionViewHeaderTag引用您提供给标题的标记号。确保它不是UICollectionView中嵌入的另一个视图的标签。

相关问题