From 147116cd043f8eb9d3f18e6ef0d91ba10724be29 Mon Sep 17 00:00:00 2001 From: Mary Baptista Martinez Date: Mon, 7 Oct 2024 13:42:28 -0700 Subject: [PATCH 1/5] usesSafeAreaLayoutGuideLeadingTrailing --- .../CollectionViewController.swift | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift b/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift index 6b813b1c..ad24685f 100644 --- a/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift +++ b/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift @@ -18,11 +18,13 @@ open class CollectionViewController: UIViewController { public init( layout: UICollectionViewLayout, sections: [SectionModel]? = nil, - configuration: CollectionViewConfiguration = .shared) + configuration: CollectionViewConfiguration = .shared, + usesSafeAreaLayoutGuideLeadingTrailing: Bool = false) { self.layout = layout initialSections = sections self.configuration = configuration + self.usesSafeAreaLayoutGuideLeadingTrailing = usesSafeAreaLayoutGuideLeadingTrailing super.init(nibName: nil, bundle: nil) } @@ -33,10 +35,11 @@ open class CollectionViewController: UIViewController { public convenience init( layout: UICollectionViewLayout, items: [ItemModeling], - configuration: CollectionViewConfiguration = .shared) + configuration: CollectionViewConfiguration = .shared, + usesSafeAreaLayoutGuideLeadingTrailing: Bool = false) { let section = SectionModel(dataID: DefaultDataID.noneProvided, items: items) - self.init(layout: layout, sections: [section], configuration: configuration) + self.init(layout: layout, sections: [section], configuration: configuration, usesSafeAreaLayoutGuideLeadingTrailing: usesSafeAreaLayoutGuideLeadingTrailing) } /// Initializes a collection view controller and configures its collection view with the provided @@ -44,9 +47,10 @@ open class CollectionViewController: UIViewController { public convenience init( layout: UICollectionViewLayout, @SectionModelBuilder sections: () -> [SectionModel], - configuration: CollectionViewConfiguration = .shared) + configuration: CollectionViewConfiguration = .shared, + usesSafeAreaLayoutGuideLeadingTrailing: Bool = false) { - self.init(layout: layout, sections: sections(), configuration: configuration) + self.init(layout: layout, sections: sections(), configuration: configuration, usesSafeAreaLayoutGuideLeadingTrailing: usesSafeAreaLayoutGuideLeadingTrailing) } /// Initializes a collection view controller and configures its collection view with the provided @@ -56,9 +60,10 @@ open class CollectionViewController: UIViewController { public convenience init( layout: UICollectionViewLayout, @ItemModelBuilder items: () -> [ItemModeling], - configuration: CollectionViewConfiguration = .shared) + configuration: CollectionViewConfiguration = .shared, + usesSafeAreaLayoutGuideLeadingTrailing: Bool = false) { - self.init(layout: layout, items: items(), configuration: configuration) + self.init(layout: layout, items: items(), configuration: configuration, usesSafeAreaLayoutGuideLeadingTrailing: usesSafeAreaLayoutGuideLeadingTrailing) } @available(*, unavailable) @@ -134,6 +139,8 @@ open class CollectionViewController: UIViewController { /// The sections that should be set on the collection view when it loads, else `nil`. private var initialSections: [SectionModel]? + private let usesSafeAreaLayoutGuideLeadingTrailing: Bool + /// Loads the collection view or returns it if already loaded. @discardableResult private func loadCollectionView() -> CollectionView { @@ -145,12 +152,17 @@ open class CollectionViewController: UIViewController { view.addSubview(collectionView) collectionView.layoutDelegate = self + collectionView.backgroundColor = .green + let layoutGuide = view.safeAreaLayoutGuide + let leadingAnchor = usesSafeAreaLayoutGuideLeadingTrailing ? layoutGuide.leadingAnchor : view.leadingAnchor + let trailingAnchor = usesSafeAreaLayoutGuideLeadingTrailing ? layoutGuide.trailingAnchor : view.trailingAnchor + collectionView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ collectionView.topAnchor.constraint(equalTo: view.topAnchor), - collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor), + collectionView.leadingAnchor.constraint(equalTo: leadingAnchor), collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor), - collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor), + collectionView.trailingAnchor.constraint(equalTo: trailingAnchor), ]) if let sections = initialSections { From 8de9f9fa569831a61d91f39ebd49a49ff2f59471 Mon Sep 17 00:00:00 2001 From: Mary Baptista Martinez Date: Mon, 7 Oct 2024 13:49:40 -0700 Subject: [PATCH 2/5] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f3333ab..080dcac5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Expose `forceLayout` in `EpoxySwiftUIHostingView` for updating the hosting view size from outside. +- CollectionViewController init param `usesSafeAreaLayoutGuideLeadingTrailing: Bool` to respect leading/trailing layoutGuide anchors which are needed for landscape orientation. Defaults to `false` and uses the view's `leadingAnchor` or `trailingAnchor`. When `true` it will use the view's `safeAreaLayoutGuide` `leadingAnchor` and `trailingAnchor`. ### Changed - `AnyItemModel` now implements the `ErasedContentProviding` protocol. From 9c0f53a0698135cccf72313ece3fe8da9cc50b29 Mon Sep 17 00:00:00 2001 From: Mary Baptista Martinez Date: Mon, 7 Oct 2024 14:00:57 -0700 Subject: [PATCH 3/5] remove green bg color --- .../ViewControllers/CollectionViewController.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift b/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift index ad24685f..d40a947a 100644 --- a/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift +++ b/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift @@ -152,7 +152,6 @@ open class CollectionViewController: UIViewController { view.addSubview(collectionView) collectionView.layoutDelegate = self - collectionView.backgroundColor = .green let layoutGuide = view.safeAreaLayoutGuide let leadingAnchor = usesSafeAreaLayoutGuideLeadingTrailing ? layoutGuide.leadingAnchor : view.leadingAnchor let trailingAnchor = usesSafeAreaLayoutGuideLeadingTrailing ? layoutGuide.trailingAnchor : view.trailingAnchor From 85ddbee883ad8ee94f710a09a8a30cb1c6304072 Mon Sep 17 00:00:00 2001 From: Mary Baptista Martinez Date: Tue, 8 Oct 2024 14:27:04 -0700 Subject: [PATCH 4/5] use configuration --- .../CollectionViewConfiguration.swift | 10 ++++++- .../CollectionViewController.swift | 26 +++++++------------ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Sources/EpoxyCollectionView/CollectionView/CollectionViewConfiguration.swift b/Sources/EpoxyCollectionView/CollectionView/CollectionViewConfiguration.swift index c721ea4e..f7e4071e 100644 --- a/Sources/EpoxyCollectionView/CollectionView/CollectionViewConfiguration.swift +++ b/Sources/EpoxyCollectionView/CollectionView/CollectionViewConfiguration.swift @@ -13,11 +13,13 @@ public struct CollectionViewConfiguration { public init( usesBatchUpdatesForAllReloads: Bool = true, usesCellPrefetching: Bool = true, - usesAccurateScrollToItem: Bool = true) + usesAccurateScrollToItem: Bool = true, + usesSafeAreaLayoutGuideLeadingTrailingAnchors: Bool = false) { self.usesBatchUpdatesForAllReloads = usesBatchUpdatesForAllReloads self.usesCellPrefetching = usesCellPrefetching self.usesAccurateScrollToItem = usesAccurateScrollToItem + self.usesSafeAreaLayoutGuideLeadingTrailingAnchors = usesSafeAreaLayoutGuideLeadingTrailingAnchors } // MARK: Public @@ -66,4 +68,10 @@ public struct CollectionViewConfiguration { /// /// - SeeAlso: `CollectionViewScrollToItemHelper` public var usesAccurateScrollToItem: Bool + + /// Respects leading and trailing safe areas from the `UILayoutGuide` when `true`. Helpful + /// for supporting landscape orientation so that content is not rendered in the notch area. + /// + /// Defaults to `false` + public var usesSafeAreaLayoutGuideLeadingTrailingAnchors: Bool } diff --git a/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift b/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift index d40a947a..42bacc8a 100644 --- a/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift +++ b/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift @@ -18,13 +18,11 @@ open class CollectionViewController: UIViewController { public init( layout: UICollectionViewLayout, sections: [SectionModel]? = nil, - configuration: CollectionViewConfiguration = .shared, - usesSafeAreaLayoutGuideLeadingTrailing: Bool = false) + configuration: CollectionViewConfiguration = .shared) { self.layout = layout initialSections = sections self.configuration = configuration - self.usesSafeAreaLayoutGuideLeadingTrailing = usesSafeAreaLayoutGuideLeadingTrailing super.init(nibName: nil, bundle: nil) } @@ -35,11 +33,10 @@ open class CollectionViewController: UIViewController { public convenience init( layout: UICollectionViewLayout, items: [ItemModeling], - configuration: CollectionViewConfiguration = .shared, - usesSafeAreaLayoutGuideLeadingTrailing: Bool = false) + configuration: CollectionViewConfiguration = .shared) { let section = SectionModel(dataID: DefaultDataID.noneProvided, items: items) - self.init(layout: layout, sections: [section], configuration: configuration, usesSafeAreaLayoutGuideLeadingTrailing: usesSafeAreaLayoutGuideLeadingTrailing) + self.init(layout: layout, sections: [section], configuration: configuration) } /// Initializes a collection view controller and configures its collection view with the provided @@ -47,10 +44,9 @@ open class CollectionViewController: UIViewController { public convenience init( layout: UICollectionViewLayout, @SectionModelBuilder sections: () -> [SectionModel], - configuration: CollectionViewConfiguration = .shared, - usesSafeAreaLayoutGuideLeadingTrailing: Bool = false) + configuration: CollectionViewConfiguration = .shared) { - self.init(layout: layout, sections: sections(), configuration: configuration, usesSafeAreaLayoutGuideLeadingTrailing: usesSafeAreaLayoutGuideLeadingTrailing) + self.init(layout: layout, sections: sections(), configuration: configuration) } /// Initializes a collection view controller and configures its collection view with the provided @@ -60,10 +56,9 @@ open class CollectionViewController: UIViewController { public convenience init( layout: UICollectionViewLayout, @ItemModelBuilder items: () -> [ItemModeling], - configuration: CollectionViewConfiguration = .shared, - usesSafeAreaLayoutGuideLeadingTrailing: Bool = false) + configuration: CollectionViewConfiguration = .shared) { - self.init(layout: layout, items: items(), configuration: configuration, usesSafeAreaLayoutGuideLeadingTrailing: usesSafeAreaLayoutGuideLeadingTrailing) + self.init(layout: layout, items: items(), configuration: configuration) } @available(*, unavailable) @@ -139,8 +134,6 @@ open class CollectionViewController: UIViewController { /// The sections that should be set on the collection view when it loads, else `nil`. private var initialSections: [SectionModel]? - private let usesSafeAreaLayoutGuideLeadingTrailing: Bool - /// Loads the collection view or returns it if already loaded. @discardableResult private func loadCollectionView() -> CollectionView { @@ -153,8 +146,9 @@ open class CollectionViewController: UIViewController { collectionView.layoutDelegate = self let layoutGuide = view.safeAreaLayoutGuide - let leadingAnchor = usesSafeAreaLayoutGuideLeadingTrailing ? layoutGuide.leadingAnchor : view.leadingAnchor - let trailingAnchor = usesSafeAreaLayoutGuideLeadingTrailing ? layoutGuide.trailingAnchor : view.trailingAnchor + let useLayoutGuide = configuration.usesSafeAreaLayoutGuideLeadingTrailingAnchors + let leadingAnchor = useLayoutGuide ? layoutGuide.leadingAnchor : view.leadingAnchor + let trailingAnchor = useLayoutGuide ? layoutGuide.trailingAnchor : view.trailingAnchor collectionView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ From 967063df9ae7a3967233c358f1d5892049301542 Mon Sep 17 00:00:00 2001 From: Mary Baptista Martinez Date: Tue, 8 Oct 2024 16:09:24 -0700 Subject: [PATCH 5/5] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 080dcac5..0f153aaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Expose `forceLayout` in `EpoxySwiftUIHostingView` for updating the hosting view size from outside. -- CollectionViewController init param `usesSafeAreaLayoutGuideLeadingTrailing: Bool` to respect leading/trailing layoutGuide anchors which are needed for landscape orientation. Defaults to `false` and uses the view's `leadingAnchor` or `trailingAnchor`. When `true` it will use the view's `safeAreaLayoutGuide` `leadingAnchor` and `trailingAnchor`. +- Added `CollectionViewConfiguration.usesSafeAreaLayoutGuideLeadingTrailingAnchors` to respect leading/trailing layoutGuide anchors which are needed for landscape orientation. Defaults to `false` and uses the view's `leadingAnchor` and `trailingAnchor`. When `true` it will use the view's `safeAreaLayoutGuide` `leadingAnchor` and `trailingAnchor`. ### Changed - `AnyItemModel` now implements the `ErasedContentProviding` protocol.