@@ -65,8 +65,8 @@ public extension SwiftUIScreen {
65
65
} ,
66
66
update: {
67
67
$0. modelSink. send ( self )
68
- $0. viewEnvironmentSink. send ( environment)
69
68
$0. swiftUIScreenSizingOptions = sizingOptions
69
+ // ViewEnvironment updates are handled by the ModeledHostingController internally
70
70
}
71
71
)
72
72
}
@@ -92,7 +92,7 @@ private struct EnvironmentInjectingView<Content: View>: View {
92
92
}
93
93
}
94
94
95
- private final class ModeledHostingController < Model, Content: View > : UIHostingController < Content > {
95
+ private final class ModeledHostingController < Model, Content: View > : UIHostingController < Content > , ViewEnvironmentObserving {
96
96
let modelSink : Sink < Model >
97
97
let viewEnvironmentSink : Sink < ViewEnvironment >
98
98
var swiftUIScreenSizingOptions : SwiftUIScreenSizingOptions {
@@ -122,6 +122,7 @@ private final class ModeledHostingController<Model, Content: View>: UIHostingCon
122
122
updateSizingOptionsIfNeeded ( )
123
123
}
124
124
125
+ @available ( * , unavailable)
125
126
required init ? ( coder aDecoder: NSCoder ) {
126
127
fatalError ( " not implemented " )
127
128
}
@@ -148,7 +149,8 @@ private final class ModeledHostingController<Model, Content: View>: UIHostingCon
148
149
// not updated appropriately after the first layout.
149
150
// UI-5797
150
151
if !hasLaidOutOnce,
151
- swiftUIScreenSizingOptions. contains ( . preferredContentSize) {
152
+ swiftUIScreenSizingOptions. contains ( . preferredContentSize)
153
+ {
152
154
let size = view. sizeThatFits ( view. frame. size)
153
155
154
156
if preferredContentSize != size {
@@ -164,13 +166,20 @@ private final class ModeledHostingController<Model, Content: View>: UIHostingCon
164
166
}
165
167
}
166
168
169
+ override func viewWillLayoutSubviews( ) {
170
+ super. viewWillLayoutSubviews ( )
171
+
172
+ applyEnvironmentIfNeeded ( )
173
+ }
174
+
167
175
private func updateSizingOptionsIfNeeded( ) {
168
176
if #available( iOS 16 . 0 , * ) {
169
177
self . sizingOptions = swiftUIScreenSizingOptions. uiHostingControllerSizingOptions
170
178
}
171
179
172
180
if !swiftUIScreenSizingOptions. contains ( . preferredContentSize) ,
173
- preferredContentSize != . zero {
181
+ preferredContentSize != . zero
182
+ {
174
183
preferredContentSize = . zero
175
184
}
176
185
}
@@ -184,11 +193,17 @@ private final class ModeledHostingController<Model, Content: View>: UIHostingCon
184
193
view. setNeedsLayout ( )
185
194
}
186
195
}
196
+
197
+ // MARK: ViewEnvironmentObserving
198
+
199
+ func apply( environment: ViewEnvironment ) {
200
+ viewEnvironmentSink. send ( environment)
201
+ }
187
202
}
188
203
189
- extension SwiftUIScreenSizingOptions {
204
+ fileprivate extension SwiftUIScreenSizingOptions {
190
205
@available ( iOS 16 . 0 , * )
191
- fileprivate var uiHostingControllerSizingOptions : UIHostingControllerSizingOptions {
206
+ var uiHostingControllerSizingOptions : UIHostingControllerSizingOptions {
192
207
var options = UIHostingControllerSizingOptions ( )
193
208
194
209
if contains ( . preferredContentSize) {
0 commit comments