Skip to content

Commit

Permalink
usesSafeAreaLayoutGuideLeadingTrailing (#174)
Browse files Browse the repository at this point in the history
* usesSafeAreaLayoutGuideLeadingTrailing

* add changelog

* remove green bg color

* use configuration

* update changelog

---------

Co-authored-by: Mary Baptista Martinez <[email protected]>
  • Loading branch information
marybm-dev and Mary Baptista Martinez authored Oct 8, 2024
1 parent 93178a5 commit 1f9b206
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1f9b206

Please sign in to comment.