在UICollectionView,我想給整個面上的均勻背景色,而不是爲單個細胞或整個集合視圖。如何更改UICollectionView中整個部分的背景顏色?
我看不出有任何的委託方法,這樣做,有什麼建議?
在UICollectionView,我想給整個面上的均勻背景色,而不是爲單個細胞或整個集合視圖。如何更改UICollectionView中整個部分的背景顏色?
我看不出有任何的委託方法,這樣做,有什麼建議?
我沒有嘗試這樣做出來呢,但在我看來,你需要使用裝飾的觀點,如果你想你的細胞後面的背景(如在圖書應用下架)。我認爲你應該能夠對每個部分有不同的觀點,並使用委託方法layoutAttributesForDecorationViewOfKind:atIndexPath:
進行設置。
在集合視圖中的每個部分可以有補充意見,所以提出補充意見每一部分然後設置背景顏色爲補充的意見,而不是部分的細胞。我希望這會有所幫助。
這是改變UICollectionView部分顏色有很大的教程:
基本上,我們將不得不繼承UICollectionViewLayoutAttributes
,UICollectionReusableView
和UICollectionViewLayout
爲了創建一個實例UICollectionReusableView作爲部分的背景視圖。
這是他的結果是:
請按照鏈接查看更多細節說明。
您附加的鏈接已更改。 http://www.ericjchapman.com/jekyll/update/ios/2014/11/10/ios-changing-section-background-color-in-UICollectionView.html – casamia
現在@casamia提供的鏈接也已經死了。這裏至少有一個到github項目的鏈接:https://github.com/ericchapman/ios_decoration_view – TylerJames
我已經改變了每個部分的背景顏色,在下面的方法非常簡單的方式:但我不知道它是否是做正確的事。但它確實有效。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FamilyCalendarCellItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"calendarItem" forIndexPath:indexPath];
Event *event;
_headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
UICollectionElementKindSectionHeader withReuseIdentifier:@"EventHeader" forIndexPath:indexPath]; //headerView is declared as property of Collection Reusable View class
if(indexPath.section==0) {
cell.backgroundColor=[UIColor orangeColor];
}
else if(indexPath.section==1) {
cell.backgroundColor=[UIColor yellowColor];
}
return cell;
}
這隻會改變單元格的背景顏色。問題是要求如何在給定部分內更改集合視圖本身的背景顏色 – TylerJames
的想法是覆蓋UICollectionViewLayoutAttributes添加顏色屬性。然後重寫UICollectionReusableView將顏色應用於視圖背景。
關閉此回購的斯威夫特3
子uicollectionreusableview
class SectionView: UICollectionReusableView {
static let kind = "sectionView"
}
子uicollectionViewFlowLayout
class CustomFlowLayout: UICollectionViewFlowLayout {
// MARK: Properties
var decorationAttributes: [IndexPath: UICollectionViewLayoutAttributes]
var sectionsWidthOrHeight: [IndexPath: CGFloat]
// MARK: Initialization
override init() {
self.decorationAttributes = [:]
self.sectionsWidthOrHeight = [:]
super.init()
}
required init?(coder aDecoder: NSCoder) {
self.decorationAttributes = [:]
self.sectionsWidthOrHeight = [:]
super.init(coder: aDecoder)
}
// MARK: Providing Layout Attributes
override func layoutAttributesForDecorationView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return self.decorationAttributes[indexPath]
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var attributes = super.layoutAttributesForElements(in: rect)
let numberOfSections = self.collectionView!.numberOfSections
var xOrYOffset = 0 as CGFloat
for sectionNumber in 0 ..< numberOfSections {
let indexPath = IndexPath(row: 0, section: sectionNumber)
let numberOfItems = self.collectionView?.numberOfItems(inSection: sectionNumber)
let sectionWidthOrHeight = numberOfItems == 0 ? UIScreen.main.bounds.height : collectionViewContentSize.height//self.sectionsWidthOrHeight[indexPath]!
let decorationAttribute = UICollectionViewLayoutAttributes(forDecorationViewOfKind: SectionView.kind, with: indexPath)
decorationAttribute.zIndex = -1
if self.scrollDirection == .vertical {
decorationAttribute.frame = CGRect(x: 0, y: xOrYOffset, width: self.collectionViewContentSize.width, height: sectionWidthOrHeight)
} else {
decorationAttribute.frame = CGRect(x: xOrYOffset, y: 0, width: sectionWidthOrHeight, height: self.collectionViewContentSize.height)
}
xOrYOffset += sectionWidthOrHeight
attributes?.append(decorationAttribute)
self.decorationAttributes[indexPath] = decorationAttribute
}
return attributes
}
}
實現這個
的CollectionView委託功能
func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
Log.printLog(identifier: elementKind, message: indexPath)
if elementKind == UICollectionElementKindSectionHeader, let view = view as? ProfileViewHeaderView {
view.backgroundColor = UIColor(red: (102/255.0), green: (169/255.0), blue: (251/255.0), alpha: 1)
} else if elementKind == SectionView.kind {
let evenSectionColor = UIColor.black
let oddSectionColor = UIColor.red
view.backgroundColor = (indexPath.section % 2 == 0) ? evenSectionColor : oddSectionColor
}
}
這是很重要
let layout = CustomFlowLayout()
layout.register(SectionView.self, forDecorationViewOfKind: SectionView.kind)
與佈局登記UICollectionReusableView沒有的CollectionView。
還有一件事。我在layoutAttributesForElements中弄亂了高度。你應該改變它爲你自己的項目。
閱讀有關補充視圖時,我只能找到用於頁眉和頁腳的示例。你有答案的例子和/或工作代碼嗎? –
我已經通過使用集合視圖自定義佈局來完成它。在自定義佈局中,我的補充視圖框架等於整個部分框架。 – Jirune
你是怎麼做到的?如果你能證明這很好。謝謝 –