diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f3333ab..0f153aaa 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. +- 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. 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 6b813b1c..42bacc8a 100644 --- a/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift +++ b/Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift @@ -145,12 +145,17 @@ open class CollectionViewController: UIViewController { view.addSubview(collectionView) collectionView.layoutDelegate = self + let layoutGuide = view.safeAreaLayoutGuide + let useLayoutGuide = configuration.usesSafeAreaLayoutGuideLeadingTrailingAnchors + let leadingAnchor = useLayoutGuide ? layoutGuide.leadingAnchor : view.leadingAnchor + let trailingAnchor = useLayoutGuide ? 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 {