diff --git a/Example/FlexLayoutSample/UI/Examples/CollectionViewExample/CollectionViewExampleView.swift b/Example/FlexLayoutSample/UI/Examples/CollectionViewExample/CollectionViewExampleView.swift index 6589811a..fc5c01c3 100644 --- a/Example/FlexLayoutSample/UI/Examples/CollectionViewExample/CollectionViewExampleView.swift +++ b/Example/FlexLayoutSample/UI/Examples/CollectionViewExample/CollectionViewExampleView.swift @@ -18,7 +18,6 @@ class CollectionViewExampleView: UIView { fileprivate let collectionView: UICollectionView fileprivate let flowLayout = UICollectionViewFlowLayout() - fileprivate let cellTemplate = HouseCell() fileprivate var houses: [House] = [] @@ -29,6 +28,7 @@ class CollectionViewExampleView: UIView { flowLayout.minimumLineSpacing = 8 flowLayout.minimumInteritemSpacing = 0 + flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize if #available(iOS 11.0, *) { flowLayout.sectionInsetReference = .fromSafeArea @@ -71,9 +71,9 @@ extension CollectionViewExampleView: UICollectionViewDelegateFlowLayout, UIColle cell.configure(house: houses[indexPath.row]) return cell } - + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { - cellTemplate.configure(house: houses[indexPath.row]) - return cellTemplate.sizeThatFits(CGSize(width: collectionView.bounds.width, height: .greatestFiniteMagnitude)) + // The height is only an estimate. HouseCell computes its actual height in preferredLayoutAttributesFitting(_:). + return CGSize(width: collectionView.bounds.width, height: 120) } } diff --git a/Example/FlexLayoutSample/UI/Examples/CollectionViewExample/HouseCell.swift b/Example/FlexLayoutSample/UI/Examples/CollectionViewExample/HouseCell.swift index 741e8c7e..8d7c7fec 100644 --- a/Example/FlexLayoutSample/UI/Examples/CollectionViewExample/HouseCell.swift +++ b/Example/FlexLayoutSample/UI/Examples/CollectionViewExample/HouseCell.swift @@ -78,11 +78,16 @@ class HouseCell: UICollectionViewCell { layout() } + override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { + let size = sizeThatFits(layoutAttributes.size) + layoutAttributes.size = size + return layoutAttributes + } + override func sizeThatFits(_ size: CGSize) -> CGSize { - contentView.pin.width(size.width) - layout() - return contentView.frame.size + return contentView.flex.sizeThatFits(size: CGSize(width: size.width, height: .nan)) } + private func layout() { contentView.flex.layout(mode: .adjustHeight) } diff --git a/Example/FlexLayoutSample/UI/Examples/TableViewExample/TableViewExampleView.swift b/Example/FlexLayoutSample/UI/Examples/TableViewExample/TableViewExampleView.swift index 7beac501..560c9ec0 100644 --- a/Example/FlexLayoutSample/UI/Examples/TableViewExample/TableViewExampleView.swift +++ b/Example/FlexLayoutSample/UI/Examples/TableViewExample/TableViewExampleView.swift @@ -17,7 +17,6 @@ import UIKit class TableViewExampleView: UIView { fileprivate let tableView = UITableView() - fileprivate let methodCellTemplate = MethodCell() fileprivate var methods: [Method] = [] diff --git a/Example/FlexLayoutSample/UI/Menu/MenuViewController.swift b/Example/FlexLayoutSample/UI/Menu/MenuViewController.swift index 9141edae..e68e0ec6 100644 --- a/Example/FlexLayoutSample/UI/Menu/MenuViewController.swift +++ b/Example/FlexLayoutSample/UI/Menu/MenuViewController.swift @@ -84,10 +84,6 @@ class MenuViewController: BaseViewController { mainView.delegate = self } -// override func viewDidAppear(_ animated: Bool) { -// super.viewDidAppear(true) -// didSelect(pageType: .collectionView) -// } } // MARK: MenuViewDelegate