From 10599ad2c4b291c50b8268d12555488739c39d05 Mon Sep 17 00:00:00 2001 From: Alan Chu Date: Thu, 6 Apr 2023 11:19:49 +0200 Subject: [PATCH 1/2] Properly constrain empty data view --- OBAKit/Mapping/MapViewController.swift | 2 ++ OBAKit/Mapping/NearbyStopsListViewController.swift | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/OBAKit/Mapping/MapViewController.swift b/OBAKit/Mapping/MapViewController.swift index f2bb26368..98da1ef31 100644 --- a/OBAKit/Mapping/MapViewController.swift +++ b/OBAKit/Mapping/MapViewController.swift @@ -393,6 +393,8 @@ class MapViewController: UIViewController, // Set a content view controller. panel.set(contentViewController: mapPanelController) + panel.contentMode = .fitToBounds + // Content Inset Adjustment + OBAListView don't play well together and causes undefined behavior, // as described in "OBAListView "sticky" row behavior while scrolling in panel" (#321) panel.contentInsetAdjustmentBehavior = .never diff --git a/OBAKit/Mapping/NearbyStopsListViewController.swift b/OBAKit/Mapping/NearbyStopsListViewController.swift index c35d98c36..958e15d9e 100644 --- a/OBAKit/Mapping/NearbyStopsListViewController.swift +++ b/OBAKit/Mapping/NearbyStopsListViewController.swift @@ -92,8 +92,8 @@ class NearbyStopsListViewController: UIViewController, UICollectionViewDelegate, private var diffableDataSource: UICollectionViewDiffableDataSource! private var cellRegistration: UICollectionView.CellRegistration! private lazy var emptyDataView: EmptyDataSetView = { - let view = EmptyDataSetView(alignment: .top) - view.translatesAutoresizingMaskIntoConstraints = false + let view = EmptyDataSetView() + view.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin] return view }() From 01243e80f9844e86d8580ae7c66424db6230e958 Mon Sep 17 00:00:00 2001 From: Alan Chu Date: Thu, 6 Apr 2023 15:02:56 +0200 Subject: [PATCH 2/2] OBAListView now uses readableContentGuide to layout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #406 — UIListContentConfiguration does not use readable content guide --- ...onLayoutSection+ReadableContentGuide.swift | 31 +++++++++++++++++++ OBAKit/Controls/ListView/OBAListView.swift | 2 +- .../NearbyStopsListViewController.swift | 30 +++++++++--------- 3 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 OBAKit/Controls/ListView/NSCollectionLayoutSection+ReadableContentGuide.swift diff --git a/OBAKit/Controls/ListView/NSCollectionLayoutSection+ReadableContentGuide.swift b/OBAKit/Controls/ListView/NSCollectionLayoutSection+ReadableContentGuide.swift new file mode 100644 index 000000000..5512996b5 --- /dev/null +++ b/OBAKit/Controls/ListView/NSCollectionLayoutSection+ReadableContentGuide.swift @@ -0,0 +1,31 @@ +// +// NSCollectionLayoutSection+ReadableContentGuide.swift +// OBAKit +// +// Created by Alan Chu on 4/7/23. +// + +import Foundation + +extension NSCollectionLayoutSection { + /// `NSCollectionLayoutSection.list` with content insets set to the Readable Content Guide. + @MainActor static func readableContentGuideList( + using configuration: UICollectionLayoutListConfiguration, + layoutEnvironment: NSCollectionLayoutEnvironment + ) -> NSCollectionLayoutSection { + let section = self.list(using: configuration, layoutEnvironment: layoutEnvironment) + + // Change the section's content insets reference to the readable content. + // This changes the way that the insets in the section's contentInsets property are interpreted. + section.contentInsetsReference = .readableContent + + // Zero out the default leading/trailing contentInsets, but preserve the default top/bottom values. + // This ensures each section will be inset horizontally exactly to the readable content width. + var contentInsets = section.contentInsets + contentInsets.leading = 0 + contentInsets.trailing = 0 + section.contentInsets = contentInsets + + return section + } +} diff --git a/OBAKit/Controls/ListView/OBAListView.swift b/OBAKit/Controls/ListView/OBAListView.swift index 62ff37c3e..18b7c118c 100644 --- a/OBAKit/Controls/ListView/OBAListView.swift +++ b/OBAKit/Controls/ListView/OBAListView.swift @@ -244,7 +244,7 @@ public class OBAListView: UICollectionView, UICollectionViewDelegate { trailingSwipeActions(for: indexPath) } - return NSCollectionLayoutSection.list(using: configuration, layoutEnvironment: environment) + return NSCollectionLayoutSection.readableContentGuideList(using: configuration, layoutEnvironment: environment) } } diff --git a/OBAKit/Mapping/NearbyStopsListViewController.swift b/OBAKit/Mapping/NearbyStopsListViewController.swift index 958e15d9e..650e51b0d 100644 --- a/OBAKit/Mapping/NearbyStopsListViewController.swift +++ b/OBAKit/Mapping/NearbyStopsListViewController.swift @@ -90,6 +90,7 @@ class NearbyStopsListViewController: UIViewController, UICollectionViewDelegate, // MARK: - UICollectionView properties private var collectionView: UICollectionView! private var diffableDataSource: UICollectionViewDiffableDataSource! + private var headerCellRegistration: UICollectionView.CellRegistration! private var cellRegistration: UICollectionView.CellRegistration! private lazy var emptyDataView: EmptyDataSetView = { let view = EmptyDataSetView() @@ -101,6 +102,13 @@ class NearbyStopsListViewController: UIViewController, UICollectionViewDelegate, override func viewDidLoad() { super.viewDidLoad() + self.headerCellRegistration = UICollectionView.CellRegistration { cell, _, item in + var config = cell.defaultContentConfiguration() + config.text = item.title + config.textProperties.font = .preferredFont(forTextStyle: .headline) + cell.contentConfiguration = config + } + self.cellRegistration = UICollectionView.CellRegistration { cell, _, item in var config = cell.defaultContentConfiguration() config.text = item.title @@ -129,25 +137,17 @@ class NearbyStopsListViewController: UIViewController, UICollectionViewDelegate, config.backgroundColor = .clear config.headerMode = .firstItemInSection - let section = NSCollectionLayoutSection.list(using: config, layoutEnvironment: layoutEnvironment) - - // Change the section's content insets reference to the readable content. - // This changes the way that the insets in the section's contentInsets property are interpreted. - section.contentInsetsReference = .readableContent - - // Zero out the default leading/trailing contentInsets, but preserve the default top/bottom values. - // This ensures each section will be inset horizontally exactly to the readable content width. - var contentInsets = section.contentInsets - contentInsets.leading = 0 - contentInsets.trailing = 0 - section.contentInsets = contentInsets - - return section + return NSCollectionLayoutSection.readableContentGuideList(using: config, layoutEnvironment: layoutEnvironment) } } private func cellFor(_ collectionView: UICollectionView, indexPath: IndexPath, itemIdentifier: ItemType) -> UICollectionViewCell? { - return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: itemIdentifier) + switch itemIdentifier.type { + case .header: + return collectionView.dequeueConfiguredReusableCell(using: headerCellRegistration, for: indexPath, item: itemIdentifier) + case .alert, .stop: + return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: itemIdentifier) + } } private func toggleEmptyDataView(isShowing: Bool) {