From 4550cd5b5cfc3f437f8ff838939c6be21edfe992 Mon Sep 17 00:00:00 2001 From: Apptek Studios <13492172+apptekstudios@users.noreply.github.com> Date: Thu, 4 Jun 2020 08:51:40 +1000 Subject: [PATCH] 1.7.1 (#153) - Workaround for SwiftUI bug (viewDidAppear not being called inside scrollView) --- ASCollectionView-SwiftUI.podspec | 2 +- Demo/ASCollectionViewDemo/MainView.swift | 8 -- .../Screens/Tags/TagsScreen.swift | 85 +++++++++---------- .../UIKit/AS_UICollectionView.swift | 16 +++- 4 files changed, 56 insertions(+), 55 deletions(-) diff --git a/ASCollectionView-SwiftUI.podspec b/ASCollectionView-SwiftUI.podspec index 886043e..52099c2 100644 --- a/ASCollectionView-SwiftUI.podspec +++ b/ASCollectionView-SwiftUI.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = 'ASCollectionView-SwiftUI' - s.version = '1.7.0' + s.version = '1.7.1' s.summary = 'A SwiftUI collection view with support for custom layouts, preloading, and more. ' s.description = <<-DESC diff --git a/Demo/ASCollectionViewDemo/MainView.swift b/Demo/ASCollectionViewDemo/MainView.swift index 1414a00..36ae6cc 100644 --- a/Demo/ASCollectionViewDemo/MainView.swift +++ b/Demo/ASCollectionViewDemo/MainView.swift @@ -58,14 +58,6 @@ struct MainView: View Text("Multiple TableView drag&drop") } } - Section(header: Text("Modified examples")) - { - NavigationLink(destination: TagsScreen(shrinkToSize: true)) - { - Image(systemName: "hammer") - Text("Tags in self-sizing collection") - } - } } .navigationBarTitle("Demo App") } diff --git a/Demo/ASCollectionViewDemo/Screens/Tags/TagsScreen.swift b/Demo/ASCollectionViewDemo/Screens/Tags/TagsScreen.swift index 08644a6..6f4f8f4 100644 --- a/Demo/ASCollectionViewDemo/Screens/Tags/TagsScreen.swift +++ b/Demo/ASCollectionViewDemo/Screens/Tags/TagsScreen.swift @@ -7,65 +7,60 @@ struct TagsScreen: View { @ObservedObject var store = TagStore() - /// Used in the extra example that shrinks the collectionView to fit its content - var shrinkToSize: Bool = false - @State var contentSize: CGSize? // This state variable is handed to the collectionView to allow it to store the content size - /// - var body: some View { - VStack(alignment: .leading, spacing: 20) - { - HStack + ScrollView(.vertical) { + VStack(alignment: .leading, spacing: 20) { - Spacer() - Text("Tap this button to reload new tags") + Text("This screen has an ASCollectionView embedded into a SwiftUI scrollview") + .multilineTextAlignment(.center) + .fixedSize(horizontal: false, vertical: true) + .frame(maxWidth: .infinity) .padding() - .background(Color(.secondarySystemBackground)) - Spacer() - } - .onTapGesture - { - withAnimation(self.shrinkToSize ? nil : .default) { + HStack + { + Spacer() + Text("Tap this button to reload new tags") + .padding() + .background(Color(.secondarySystemBackground)) + Spacer() + } + .onTapGesture + { self.store.refreshStore() } - } - Text("Tags:") - .font(.title) - - ASCollectionView( - section: - ASCollectionViewSection(id: 0, data: store.items) - { item, _ in - Text(item.displayString) - .fixedSize(horizontal: false, vertical: true) - .padding(5) - .background(Color(.systemGray)) - .cornerRadius(5) - }.selfSizingConfig { _ in - ASSelfSizingConfig(canExceedCollectionWidth: false) + Text("Tags:") + .font(.title) + + ASCollectionView( + section: + ASCollectionViewSection(id: 0, data: store.items) + { item, _ in + Text(item.displayString) + .fixedSize(horizontal: false, vertical: true) + .padding(5) + .background(Color(.systemGray)) + .cornerRadius(5) + }.selfSizingConfig { _ in + ASSelfSizingConfig(canExceedCollectionWidth: false) + } + ) + .layout + { + let fl = AlignedFlowLayout() + fl.estimatedItemSize = UICollectionViewFlowLayout.automaticSize + return fl } - ) - .layout - { - let fl = AlignedFlowLayout() - fl.estimatedItemSize = UICollectionViewFlowLayout.automaticSize - return fl - } - .shrinkToContentSize(isEnabled: shrinkToSize, dimension: .vertical) - - if shrinkToSize - { + .fitContentSize(dimension: .vertical) Text("This is another view in the VStack, it shows how the collectionView above fits itself to the content.") .padding() - .frame(idealWidth: .infinity, maxWidth: .infinity) + .frame(maxWidth: .infinity) .foregroundColor(Color(.secondaryLabel)) .fixedSize(horizontal: false, vertical: true) .background(Color(.secondarySystemBackground)) - Spacer() } + .padding() } - .padding() .navigationBarTitle("Tags", displayMode: .inline) } } diff --git a/Sources/ASCollectionView/UIKit/AS_UICollectionView.swift b/Sources/ASCollectionView/UIKit/AS_UICollectionView.swift index 16d6b0c..051b9bd 100644 --- a/Sources/ASCollectionView/UIKit/AS_UICollectionView.swift +++ b/Sources/ASCollectionView/UIKit/AS_UICollectionView.swift @@ -7,10 +7,17 @@ import SwiftUI public class AS_CollectionViewController: UIViewController { weak var coordinator: ASCollectionViewCoordinator? + { + didSet + { + collectionView.coordinator = coordinator + } + } var collectionViewLayout: UICollectionViewLayout lazy var collectionView: AS_UICollectionView = { let cv = AS_UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout) + cv.coordinator = coordinator return cv }() @@ -95,4 +102,11 @@ public class AS_CollectionViewController: UIViewController } @available(iOS 13.0, *) -class AS_UICollectionView: UICollectionView {} +class AS_UICollectionView: UICollectionView +{ + weak var coordinator: ASCollectionViewCoordinator? + override func didMoveToSuperview() + { + if superview != nil { coordinator?.onMoveToParent() } + } +}