Skip to content

Commit 87f135f

Browse files
committed
Added widget wrapper.
1 parent d64b4bf commit 87f135f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Sources/SwiftUIExtension/Extensions/ViewExtension.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ extension View {
99
public func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View {
1010
self.modifier(DeviceRotationViewModifier(action: action))
1111
}
12+
13+
public func containerBackgroundForWidget<Background>(background: @escaping () -> Background) -> some View where Background: View {
14+
modifier(ContainerBackgroundForWidgetModifier(background: background))
15+
}
1216
}
1317

1418
struct DeviceRotationViewModifier: ViewModifier {
@@ -23,3 +27,23 @@ struct DeviceRotationViewModifier: ViewModifier {
2327
}
2428
}
2529
}
30+
31+
struct ContainerBackgroundForWidgetModifier<Background>: ViewModifier where Background: View {
32+
33+
let background: () -> Background
34+
35+
func body(content: Content) -> some View {
36+
if #available(iOS 17.0, iOSApplicationExtension 17.0, watchOS 10.0, watchOSApplicationExtension 10.0, macOS 14.0, *) {
37+
content
38+
.containerBackground(for: .widget) {
39+
background()
40+
}
41+
} else {
42+
ZStack {
43+
background()
44+
content
45+
.padding()
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)