Skip to content

Commit e98b133

Browse files
committed
Fixed modifier for device orientation.
1 parent 87f135f commit e98b133

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

Sources/SwiftUIExtension/Extensions/ViewExtension.swift

+25-18
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,11 @@ extension View {
66
self.onReceive(NotificationCenter.default.publisher(for: name), perform: action)
77
}
88

9-
public func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View {
10-
self.modifier(DeviceRotationViewModifier(action: action))
11-
}
12-
13-
public func containerBackgroundForWidget<Background>(background: @escaping () -> Background) -> some View where Background: View {
9+
public func containerBackgroundForWidget<Background>(@ViewBuilder background: @escaping () -> Background) -> some View where Background: View {
1410
modifier(ContainerBackgroundForWidgetModifier(background: background))
1511
}
1612
}
1713

18-
struct DeviceRotationViewModifier: ViewModifier {
19-
20-
let action: (UIDeviceOrientation) -> Void
21-
22-
func body(content: Content) -> some View {
23-
content
24-
.onAppear()
25-
.onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
26-
action(UIDevice.current.orientation)
27-
}
28-
}
29-
}
30-
3114
struct ContainerBackgroundForWidgetModifier<Background>: ViewModifier where Background: View {
3215

3316
let background: () -> Background
@@ -47,3 +30,27 @@ struct ContainerBackgroundForWidgetModifier<Background>: ViewModifier where Back
4730
}
4831
}
4932
}
33+
34+
// MARK: - Only iOS
35+
36+
#if os(iOS)
37+
extension View {
38+
39+
public func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View {
40+
self.modifier(DeviceRotationViewModifier(action: action))
41+
}
42+
}
43+
44+
struct DeviceRotationViewModifier: ViewModifier {
45+
46+
let action: (UIDeviceOrientation) -> Void
47+
48+
func body(content: Content) -> some View {
49+
content
50+
.onAppear()
51+
.onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
52+
action(UIDevice.current.orientation)
53+
}
54+
}
55+
}
56+
#endif

0 commit comments

Comments
 (0)