在UICollectionView,我想给整个面上的均匀背景色,而不是为单个细胞或整个集合视图。
我看不出有任何的委托方法,这样做,有什么建议?
在UICollectionView,我想给整个面上的均匀背景色,而不是为单个细胞或整个集合视图。
我看不出有任何的委托方法,这样做,有什么建议?
这是改变UICollectionView部分颜色有很大的教程:
http://www.ericjchapman.com/jekyll/update/ios/2014/11/10/ios-changing-section-background-color-in-UICollectionView.html
基本上,我们将不得不继承UICollectionViewLayoutAttributes
, UICollectionReusableView
和UICollectionViewLayout
为了创建一个实例UICollectionReusableView作为节的背景图。
这是他的结果是:
请按照链接查看更多细节说明。
我们的想法是覆盖UICollectionViewLayoutAttributes添加颜色属性。 然后覆盖UICollectionReusableView应用颜色到视图背景。
https://github.com/strawberrycode/SCSectionBackground
我没有试过了没有,但在我看来,你需要使用装饰的观点,如果你想你的细胞后面的背景(如在图书应用下架)。 我想你应该能够对各部分不同的看法,并使用委托方法设置它们layoutAttributesForDecorationViewOfKind:atIndexPath:
在集合视图中的每个部分可以有补充意见,所以提出补充意见每一部分然后设置背景颜色为补充的意见,而不是部分的细胞。 我希望这将有助于。
我去了这个回购在这里下车https://github.com/SebastienMichoy/CollectionViewsDemo/tree/master/CollectionViewsDemo/Sources/Collections%20Views
斯威夫特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)
注册布局没有的CollectionView的UICollectionReusableView。
还有一件事。 我搞砸周围的layoutAttributesForElements高度。 你应该改变它自己的项目。
我已经改变了每个部分的背景颜色,在下面的方法非常简单的方式:但我不知道它是否是做正确的事。 但它确实工作。
- (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;
}