Skip to content

Commit a3cab32

Browse files
committed
Support dynamic content updates in SwiftUI
Store content hosting controller reference and update it dynamically in updateUIViewController(), allowing panel content to change without recreation. Resolved #672
1 parent 5fff51b commit a3cab32

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

Examples/SamplesSwiftUI/SamplesSwiftUI/UseCases/MainView.swift

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,28 @@ import SwiftUI
55
import UIKit
66

77
struct MainView: View {
8+
enum CardContent: String, CaseIterable, Identifiable {
9+
case list
10+
case detail
11+
12+
var id: String { rawValue }
13+
}
814
@State private var panelLayout: FloatingPanelLayout? = MyFloatingPanelLayout()
915
@State private var panelState: FloatingPanelState?
16+
@State private var selectedContent: CardContent = .list
1017

1118
var body: some View {
1219
ZStack {
1320
Color.orange
1421
.ignoresSafeArea()
15-
.floatingPanel(
16-
coordinator: MyPanelCoordinator.self
17-
) { proxy in
18-
ContentView(proxy: proxy)
19-
}
20-
.floatingPanelSurfaceAppearance(.transparent())
21-
.floatingPanelLayout(panelLayout)
22-
.floatingPanelState($panelState)
23-
2422
VStack(spacing: 32) {
23+
Picker("type", selection: $selectedContent) {
24+
ForEach(CardContent.allCases) {
25+
type in
26+
Text(type.rawValue).tag(type)
27+
}
28+
}
29+
.pickerStyle(.segmented)
2530
Button("Move to full") {
2631
withAnimation(.interactiveSpring) {
2732
panelState = .full
@@ -42,8 +47,26 @@ struct MainView: View {
4247
Text("Switch to My layout")
4348
}
4449
}
50+
Spacer()
51+
}
52+
}
53+
.floatingPanel(
54+
coordinator: MyPanelCoordinator.self
55+
) { proxy in
56+
switch selectedContent {
57+
case .list:
58+
ContentView(proxy: proxy)
59+
case .detail:
60+
VStack {
61+
Text("off")
62+
.padding(.top, 32)
63+
Spacer()
64+
}
4565
}
4666
}
67+
.floatingPanelSurfaceAppearance(.transparent())
68+
.floatingPanelLayout(panelLayout)
69+
.floatingPanelState($panelState)
4770
}
4871
}
4972

Sources/SwiftUI/FloatingPanelView.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ struct FloatingPanelView<MainView: View, ContentView: View>: UIViewControllerRep
110110
_ uiViewController: UIHostingController<MainView>,
111111
context: Context
112112
) {
113+
uiViewController.rootView = main
114+
115+
context.coordinator.updateContent(content(context.coordinator.proxy))
113116
context.coordinator.onUpdate(context: context)
117+
114118
applyEnvironment(context: context)
115119
applyAnimatableEnvironment(context: context)
116120
}
@@ -160,6 +164,9 @@ class FloatingPanelCoordinatorProxy {
160164

161165
private var subscriptions: Set<AnyCancellable> = Set()
162166

167+
// Store a reference to the content hosting controller for dynamic updates
168+
private weak var contentHostingController: UIViewController?
169+
163170
var proxy: FloatingPanelProxy { origin.proxy }
164171
var controller: FloatingPanelController { origin.controller }
165172

@@ -181,12 +188,25 @@ class FloatingPanelCoordinatorProxy {
181188
mainHostingController: UIHostingController<Main>,
182189
contentHostingController: UIHostingController<Content>
183190
) {
191+
// Store the content hosting controller reference
192+
self.contentHostingController = contentHostingController
193+
184194
origin.setupFloatingPanel(
185195
mainHostingController: mainHostingController,
186196
contentHostingController: contentHostingController
187197
)
188198
}
189199

200+
/// Updates the content of the floating panel with new content.
201+
func updateContent<Content: View>(_ newContent: Content) {
202+
guard
203+
let hostingController = contentHostingController as? UIHostingController<Content>
204+
else {
205+
return
206+
}
207+
hostingController.rootView = newContent
208+
}
209+
190210
func onUpdate<Representable>(
191211
context: UIViewControllerRepresentableContext<Representable>
192212
) where Representable: UIViewControllerRepresentable {

0 commit comments

Comments
 (0)