|
@@ -10,28 +10,44 @@ struct _VibrancyVisualEffectViewRepresentable<Content: View>: UIViewRepresentabl
|
|
|
self.content = content
|
|
|
}
|
|
|
|
|
|
+ func makeCoordinator() -> Coordinator {
|
|
|
+ Coordinator(content: content)
|
|
|
+ }
|
|
|
+
|
|
|
func makeUIView(context: Context) -> UIVisualEffectView {
|
|
|
- let blurEffect = UIBlurEffect(style: context.environment.blurEffectStyle)
|
|
|
- let visualEffectView = UIVisualEffectView()
|
|
|
-
|
|
|
- // Set `visualEffectView`'s `effect`.
|
|
|
- if let vibrancyEffectStyle = context.environment.vibrancyEffectStyle {
|
|
|
- visualEffectView.effect = UIVibrancyEffect(blurEffect: blurEffect, style: vibrancyEffectStyle)
|
|
|
- } else {
|
|
|
- visualEffectView.effect = UIVibrancyEffect(blurEffect: blurEffect)
|
|
|
- }
|
|
|
-
|
|
|
- // Embed `content` in `visualEffectView`'s `contentView`.
|
|
|
- let hostingControllerView = UIHostingController(rootView: content).view!
|
|
|
- // Center `hostingControllerView`'s frame (at the cost of losing its intrinsic content size).
|
|
|
- hostingControllerView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
|
- hostingControllerView.backgroundColor = .clear
|
|
|
- visualEffectView.contentView.addSubview(hostingControllerView)
|
|
|
-
|
|
|
- return visualEffectView
|
|
|
+ context.coordinator.configuredVisualEffectView(from: context)
|
|
|
}
|
|
|
|
|
|
- func updateUIView(_ uiView: UIVisualEffectView, context: Context) {}
|
|
|
+ func updateUIView(_ uiView: UIVisualEffectView, context: Context) {
|
|
|
+ context.coordinator.configuredVisualEffectView(from: context)
|
|
|
+ }
|
|
|
|
|
|
private let content: Content
|
|
|
}
|
|
|
+
|
|
|
+extension _VibrancyVisualEffectViewRepresentable {
|
|
|
+ final class Coordinator {
|
|
|
+ init(content: Content) {
|
|
|
+ let hostingController = UIHostingController(rootView: content)
|
|
|
+ hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
|
+ hostingController.view.backgroundColor = nil
|
|
|
+
|
|
|
+ visualEffectView.contentView.addSubview(hostingController.view)
|
|
|
+ }
|
|
|
+
|
|
|
+ @discardableResult func configuredVisualEffectView(from context: Context) -> UIVisualEffectView {
|
|
|
+ let blurEffect = UIBlurEffect(style: context.environment.blurEffectStyle)
|
|
|
+
|
|
|
+ // Set `visualEffectView`'s `effect`.
|
|
|
+ if let vibrancyEffectStyle = context.environment.vibrancyEffectStyle {
|
|
|
+ visualEffectView.effect = UIVibrancyEffect(blurEffect: blurEffect, style: vibrancyEffectStyle)
|
|
|
+ } else {
|
|
|
+ visualEffectView.effect = UIVibrancyEffect(blurEffect: blurEffect)
|
|
|
+ }
|
|
|
+
|
|
|
+ return visualEffectView
|
|
|
+ }
|
|
|
+
|
|
|
+ private let visualEffectView = UIVisualEffectView()
|
|
|
+ }
|
|
|
+}
|