Skip to content

Commit 1f9b206

Browse files
marybm-devMary Baptista Martinez
andauthored
usesSafeAreaLayoutGuideLeadingTrailing (#174)
* usesSafeAreaLayoutGuideLeadingTrailing * add changelog * remove green bg color * use configuration * update changelog --------- Co-authored-by: Mary Baptista Martinez <mary.baptistamartinez@airbnb.com>
1 parent 93178a5 commit 1f9b206

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010
- Expose `forceLayout` in `EpoxySwiftUIHostingView` for updating the hosting view size from outside.
11+
- 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`.
1112

1213
### Changed
1314
- `AnyItemModel` now implements the `ErasedContentProviding` protocol.

Sources/EpoxyCollectionView/CollectionView/CollectionViewConfiguration.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ public struct CollectionViewConfiguration {
1313
public init(
1414
usesBatchUpdatesForAllReloads: Bool = true,
1515
usesCellPrefetching: Bool = true,
16-
usesAccurateScrollToItem: Bool = true)
16+
usesAccurateScrollToItem: Bool = true,
17+
usesSafeAreaLayoutGuideLeadingTrailingAnchors: Bool = false)
1718
{
1819
self.usesBatchUpdatesForAllReloads = usesBatchUpdatesForAllReloads
1920
self.usesCellPrefetching = usesCellPrefetching
2021
self.usesAccurateScrollToItem = usesAccurateScrollToItem
22+
self.usesSafeAreaLayoutGuideLeadingTrailingAnchors = usesSafeAreaLayoutGuideLeadingTrailingAnchors
2123
}
2224

2325
// MARK: Public
@@ -66,4 +68,10 @@ public struct CollectionViewConfiguration {
6668
///
6769
/// - SeeAlso: `CollectionViewScrollToItemHelper`
6870
public var usesAccurateScrollToItem: Bool
71+
72+
/// Respects leading and trailing safe areas from the `UILayoutGuide` when `true`. Helpful
73+
/// for supporting landscape orientation so that content is not rendered in the notch area.
74+
///
75+
/// Defaults to `false`
76+
public var usesSafeAreaLayoutGuideLeadingTrailingAnchors: Bool
6977
}

Sources/EpoxyCollectionView/ViewControllers/CollectionViewController.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,17 @@ open class CollectionViewController: UIViewController {
145145
view.addSubview(collectionView)
146146
collectionView.layoutDelegate = self
147147

148+
let layoutGuide = view.safeAreaLayoutGuide
149+
let useLayoutGuide = configuration.usesSafeAreaLayoutGuideLeadingTrailingAnchors
150+
let leadingAnchor = useLayoutGuide ? layoutGuide.leadingAnchor : view.leadingAnchor
151+
let trailingAnchor = useLayoutGuide ? layoutGuide.trailingAnchor : view.trailingAnchor
152+
148153
collectionView.translatesAutoresizingMaskIntoConstraints = false
149154
NSLayoutConstraint.activate([
150155
collectionView.topAnchor.constraint(equalTo: view.topAnchor),
151-
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
156+
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
152157
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
153-
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
158+
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
154159
])
155160

156161
if let sections = initialSections {

0 commit comments

Comments
 (0)