|
| 1 | +// Copyright 2018-Present Shin Yamamoto. All rights reserved. MIT license. |
| 2 | + |
| 3 | +import UIKit |
| 4 | +import FloatingPanel |
| 5 | + |
| 6 | +@available(iOS 13.0, *) |
| 7 | +class CollectionViewControllerForAdaptiveLayout: UIViewController { |
| 8 | + class PanelLayout: FloatingPanelLayout { |
| 9 | + let position: FloatingPanelPosition = .bottom |
| 10 | + let initialState: FloatingPanelState = .full |
| 11 | + |
| 12 | + private unowned var targetGuide: UILayoutGuide |
| 13 | + |
| 14 | + init(targetGuide: UILayoutGuide) { |
| 15 | + self.targetGuide = targetGuide |
| 16 | + } |
| 17 | + |
| 18 | + var anchors: [FloatingPanelState : FloatingPanelLayoutAnchoring] { |
| 19 | + return [ |
| 20 | + .full: FloatingPanelAdaptiveLayoutAnchor( |
| 21 | + absoluteOffset: 0.0, |
| 22 | + contentLayout: targetGuide, |
| 23 | + referenceGuide: .superview, |
| 24 | + contentBoundingGuide: .safeArea |
| 25 | + ), |
| 26 | + .half: FloatingPanelAdaptiveLayoutAnchor( |
| 27 | + fractionalOffset: 0.5, |
| 28 | + contentLayout: targetGuide, |
| 29 | + referenceGuide: .superview, |
| 30 | + contentBoundingGuide: .safeArea |
| 31 | + ), |
| 32 | + ] |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + enum LayoutType { |
| 37 | + case flow |
| 38 | + case compositional |
| 39 | + } |
| 40 | + |
| 41 | + weak var collectionView: UICollectionView! |
| 42 | + var layoutType: LayoutType = .flow |
| 43 | + |
| 44 | + override func viewDidLoad() { |
| 45 | + super.viewDidLoad() |
| 46 | + setupCollectionView() |
| 47 | + } |
| 48 | + |
| 49 | + private func setupCollectionView() { |
| 50 | + let collectionViewLayout = { |
| 51 | + switch layoutType { |
| 52 | + case .flow: |
| 53 | + CollectionViewLayoutFactory.flowLayout |
| 54 | + case .compositional: |
| 55 | + CollectionViewLayoutFactory.compositionalLayout |
| 56 | + } |
| 57 | + }() |
| 58 | + let collectionView = IntrinsicCollectionView( |
| 59 | + frame: .zero, |
| 60 | + collectionViewLayout: collectionViewLayout |
| 61 | + ) |
| 62 | + |
| 63 | + collectionView.delegate = self |
| 64 | + collectionView.dataSource = self |
| 65 | + collectionView.backgroundColor = .yellow |
| 66 | + collectionView.register(Cell.self, forCellWithReuseIdentifier: Cell.reuseIdentifier) |
| 67 | + |
| 68 | + view.addSubview(collectionView) |
| 69 | + self.collectionView = collectionView |
| 70 | + |
| 71 | + collectionView.translatesAutoresizingMaskIntoConstraints = false |
| 72 | + NSLayoutConstraint.activate([ |
| 73 | + collectionView.topAnchor.constraint(equalTo: view.topAnchor), |
| 74 | + collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor), |
| 75 | + collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor), |
| 76 | + collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor) |
| 77 | + ]) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +@available(iOS 13.0, *) |
| 82 | +extension CollectionViewControllerForAdaptiveLayout: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { |
| 83 | + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
| 84 | + return 5 // Only three cells needed to fill the space |
| 85 | + } |
| 86 | + |
| 87 | + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
| 88 | + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.reuseIdentifier, for: indexPath) as! Cell |
| 89 | + cell.configure(text: "Item \(indexPath.row)") |
| 90 | + return cell |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { |
| 95 | + let width = collectionView.frame.width |
| 96 | + return CGSize(width: width, height: 100) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +@available(iOS 13.0, *) |
| 101 | +extension CollectionViewControllerForAdaptiveLayout { |
| 102 | + enum CollectionViewLayoutFactory { |
| 103 | + static var flowLayout: UICollectionViewLayout { |
| 104 | + let layout = UICollectionViewFlowLayout() |
| 105 | + layout.scrollDirection = .vertical |
| 106 | + layout.minimumLineSpacing = 8 // Vertical spacing between rows |
| 107 | + return layout |
| 108 | + } |
| 109 | + |
| 110 | + @available(iOS 13.0, *) |
| 111 | + static var compositionalLayout: UICollectionViewLayout { |
| 112 | + UICollectionViewCompositionalLayout { (sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in |
| 113 | + let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(100)) |
| 114 | + let item = NSCollectionLayoutItem(layoutSize: itemSize) |
| 115 | + |
| 116 | + let group = NSCollectionLayoutGroup.horizontal(layoutSize: itemSize, subitems: [item]) |
| 117 | + |
| 118 | + let section = NSCollectionLayoutSection(group: group) |
| 119 | + section.interGroupSpacing = 8 // Spacing between each group/item |
| 120 | + section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0) |
| 121 | + |
| 122 | + return section |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + private final class Cell: UICollectionViewCell { |
| 129 | + static let reuseIdentifier = "Cell" |
| 130 | + |
| 131 | + private let label: UILabel = { |
| 132 | + let label = UILabel() |
| 133 | + label.textAlignment = .center |
| 134 | + label.textColor = .white |
| 135 | + return label |
| 136 | + }() |
| 137 | + |
| 138 | + override init(frame: CGRect) { |
| 139 | + super.init(frame: frame) |
| 140 | + commonInit() |
| 141 | + } |
| 142 | + |
| 143 | + required init?(coder: NSCoder) { |
| 144 | + super.init(coder: coder) |
| 145 | + commonInit() |
| 146 | + } |
| 147 | + |
| 148 | + private func commonInit() { |
| 149 | + backgroundColor = .systemBlue |
| 150 | + addSubview(label) |
| 151 | + label.translatesAutoresizingMaskIntoConstraints = false |
| 152 | + NSLayoutConstraint.activate([ |
| 153 | + label.centerXAnchor.constraint(equalTo: centerXAnchor), |
| 154 | + label.centerYAnchor.constraint(equalTo: centerYAnchor) |
| 155 | + ]) |
| 156 | + } |
| 157 | + |
| 158 | + func configure(text: String) { |
| 159 | + label.text = text |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private final class IntrinsicCollectionView: UICollectionView { |
| 164 | + override public var contentSize: CGSize { |
| 165 | + didSet { |
| 166 | + invalidateIntrinsicContentSize() |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + override public var intrinsicContentSize: CGSize { |
| 171 | + layoutIfNeeded() |
| 172 | + return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height) |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments