Skip to content
This repository was archived by the owner on Nov 4, 2022. It is now read-only.

Commit 4550cd5

Browse files
1.7.1 (#153)
- Workaround for SwiftUI bug (viewDidAppear not being called inside scrollView)
1 parent e71eda8 commit 4550cd5

File tree

4 files changed

+56
-55
lines changed

4 files changed

+56
-55
lines changed

ASCollectionView-SwiftUI.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Pod::Spec.new do |s|
33
s.name = 'ASCollectionView-SwiftUI'
4-
s.version = '1.7.0'
4+
s.version = '1.7.1'
55
s.summary = 'A SwiftUI collection view with support for custom layouts, preloading, and more. '
66

77
s.description = <<-DESC

Demo/ASCollectionViewDemo/MainView.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,6 @@ struct MainView: View
5858
Text("Multiple TableView drag&drop")
5959
}
6060
}
61-
Section(header: Text("Modified examples"))
62-
{
63-
NavigationLink(destination: TagsScreen(shrinkToSize: true))
64-
{
65-
Image(systemName: "hammer")
66-
Text("Tags in self-sizing collection")
67-
}
68-
}
6961
}
7062
.navigationBarTitle("Demo App")
7163
}

Demo/ASCollectionViewDemo/Screens/Tags/TagsScreen.swift

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,65 +7,60 @@ struct TagsScreen: View
77
{
88
@ObservedObject var store = TagStore()
99

10-
/// Used in the extra example that shrinks the collectionView to fit its content
11-
var shrinkToSize: Bool = false
12-
@State var contentSize: CGSize? // This state variable is handed to the collectionView to allow it to store the content size
13-
///
14-
1510
var body: some View
1611
{
17-
VStack(alignment: .leading, spacing: 20)
18-
{
19-
HStack
12+
ScrollView(.vertical) {
13+
VStack(alignment: .leading, spacing: 20)
2014
{
21-
Spacer()
22-
Text("Tap this button to reload new tags")
15+
Text("This screen has an ASCollectionView embedded into a SwiftUI scrollview")
16+
.multilineTextAlignment(.center)
17+
.fixedSize(horizontal: false, vertical: true)
18+
.frame(maxWidth: .infinity)
2319
.padding()
24-
.background(Color(.secondarySystemBackground))
25-
Spacer()
26-
}
27-
.onTapGesture
28-
{
29-
withAnimation(self.shrinkToSize ? nil : .default) {
20+
HStack
21+
{
22+
Spacer()
23+
Text("Tap this button to reload new tags")
24+
.padding()
25+
.background(Color(.secondarySystemBackground))
26+
Spacer()
27+
}
28+
.onTapGesture
29+
{
3030
self.store.refreshStore()
3131
}
32-
}
33-
Text("Tags:")
34-
.font(.title)
35-
36-
ASCollectionView(
37-
section:
38-
ASCollectionViewSection(id: 0, data: store.items)
39-
{ item, _ in
40-
Text(item.displayString)
41-
.fixedSize(horizontal: false, vertical: true)
42-
.padding(5)
43-
.background(Color(.systemGray))
44-
.cornerRadius(5)
45-
}.selfSizingConfig { _ in
46-
ASSelfSizingConfig(canExceedCollectionWidth: false)
32+
Text("Tags:")
33+
.font(.title)
34+
35+
ASCollectionView(
36+
section:
37+
ASCollectionViewSection(id: 0, data: store.items)
38+
{ item, _ in
39+
Text(item.displayString)
40+
.fixedSize(horizontal: false, vertical: true)
41+
.padding(5)
42+
.background(Color(.systemGray))
43+
.cornerRadius(5)
44+
}.selfSizingConfig { _ in
45+
ASSelfSizingConfig(canExceedCollectionWidth: false)
46+
}
47+
)
48+
.layout
49+
{
50+
let fl = AlignedFlowLayout()
51+
fl.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
52+
return fl
4753
}
48-
)
49-
.layout
50-
{
51-
let fl = AlignedFlowLayout()
52-
fl.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
53-
return fl
54-
}
55-
.shrinkToContentSize(isEnabled: shrinkToSize, dimension: .vertical)
56-
57-
if shrinkToSize
58-
{
54+
.fitContentSize(dimension: .vertical)
5955
Text("This is another view in the VStack, it shows how the collectionView above fits itself to the content.")
6056
.padding()
61-
.frame(idealWidth: .infinity, maxWidth: .infinity)
57+
.frame(maxWidth: .infinity)
6258
.foregroundColor(Color(.secondaryLabel))
6359
.fixedSize(horizontal: false, vertical: true)
6460
.background(Color(.secondarySystemBackground))
65-
Spacer()
6661
}
62+
.padding()
6763
}
68-
.padding()
6964
.navigationBarTitle("Tags", displayMode: .inline)
7065
}
7166
}

Sources/ASCollectionView/UIKit/AS_UICollectionView.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ import SwiftUI
77
public class AS_CollectionViewController: UIViewController
88
{
99
weak var coordinator: ASCollectionViewCoordinator?
10+
{
11+
didSet
12+
{
13+
collectionView.coordinator = coordinator
14+
}
15+
}
1016

1117
var collectionViewLayout: UICollectionViewLayout
1218
lazy var collectionView: AS_UICollectionView = {
1319
let cv = AS_UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
20+
cv.coordinator = coordinator
1421
return cv
1522
}()
1623

@@ -95,4 +102,11 @@ public class AS_CollectionViewController: UIViewController
95102
}
96103

97104
@available(iOS 13.0, *)
98-
class AS_UICollectionView: UICollectionView {}
105+
class AS_UICollectionView: UICollectionView
106+
{
107+
weak var coordinator: ASCollectionViewCoordinator?
108+
override func didMoveToSuperview()
109+
{
110+
if superview != nil { coordinator?.onMoveToParent() }
111+
}
112+
}

0 commit comments

Comments
 (0)