Created
July 16, 2021 18:56
-
-
Save bondxf/9b197dcee2ab0ad03c97f3b804a95bcd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class SectionBackgroundDecorationView: UICollectionReusableView { | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
configure() | |
} | |
required init?(coder: NSCoder) { | |
fatalError("not implemented") | |
} | |
} | |
extension SectionBackgroundDecorationView { | |
func configure() { | |
backgroundColor = UIColor.lightGray.withAlphaComponent(0.5) | |
layer.borderColor = UIColor.black.cgColor | |
layer.borderWidth = 1 | |
layer.cornerRadius = 12 | |
} | |
} | |
class SectionDecorationViewController: UIViewController { | |
static let sectionBackgroundDecorationElementKind = "section-background-element-kind" | |
var currentSnapshot: NSDiffableDataSourceSnapshot<Int, Int>! = nil | |
var dataSource: UICollectionViewDiffableDataSource<Int, Int>! = nil | |
var collectionView: UICollectionView! = nil | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
navigationItem.title = "Section Background Decoration View" | |
configureHierarchy() | |
configureDataSource() | |
} | |
} | |
extension SectionDecorationViewController { | |
/// - Tag: Background | |
func createLayout() -> UICollectionViewLayout { | |
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), | |
heightDimension: .fractionalHeight(1.0)) | |
let item = NSCollectionLayoutItem(layoutSize: itemSize) | |
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), | |
heightDimension: .absolute(44)) | |
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) | |
let section = NSCollectionLayoutSection(group: group) | |
section.interGroupSpacing = 5 | |
section.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10) | |
let decoration = NSCollectionLayoutDecorationItem.background(elementKind: Self.sectionBackgroundDecorationElementKind) | |
section.decorationItems = [decoration] | |
print("before change \(section.decorationItems)") | |
decoration.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 5, bottom: 5, trailing: 5) | |
print("after change \(section.decorationItems)") | |
let layout = UICollectionViewCompositionalLayout(section: section) | |
layout.register(SectionBackgroundDecorationView.self, forDecorationViewOfKind: Self.sectionBackgroundDecorationElementKind) | |
return layout | |
} | |
} | |
extension SectionDecorationViewController { | |
func configureHierarchy() { | |
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout()) | |
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight] | |
collectionView.backgroundColor = .systemBackground | |
view.addSubview(collectionView) | |
} | |
func configureDataSource() { | |
let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Int> { (cell, indexPath, identifier) in | |
var config = cell.defaultContentConfiguration() | |
config.text = "\(indexPath.section),\(indexPath.item)" | |
cell.contentConfiguration = config | |
} | |
dataSource = UICollectionViewDiffableDataSource<Int, Int>(collectionView: collectionView) { | |
(collectionView: UICollectionView, indexPath: IndexPath, identifier: Int) -> UICollectionViewCell? in | |
// Return the cell. | |
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: identifier) | |
} | |
// initial data | |
let itemsPerSection = 5 | |
let sections = Array(0..<5) | |
currentSnapshot = NSDiffableDataSourceSnapshot<Int, Int>() | |
var itemOffset = 0 | |
sections.forEach { | |
currentSnapshot.appendSections([$0]) | |
currentSnapshot.appendItems(Array(itemOffset..<itemOffset + itemsPerSection)) | |
itemOffset += itemsPerSection | |
} | |
dataSource.apply(currentSnapshot, animatingDifferences: false) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment