2012-11-23 42 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中嵌入的另一個視圖的標籤。

相關問題