diff --git a/Sources/SwiftUIExtension/Extensions/ViewExtension.swift b/Sources/SwiftUIExtension/Extensions/ViewExtension.swift index a620766..554bcf1 100644 --- a/Sources/SwiftUIExtension/Extensions/ViewExtension.swift +++ b/Sources/SwiftUIExtension/Extensions/ViewExtension.swift @@ -6,28 +6,11 @@ extension View { self.onReceive(NotificationCenter.default.publisher(for: name), perform: action) } - public func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View { - self.modifier(DeviceRotationViewModifier(action: action)) - } - - public func containerBackgroundForWidget(background: @escaping () -> Background) -> some View where Background: View { + public func containerBackgroundForWidget(@ViewBuilder background: @escaping () -> Background) -> some View where Background: View { modifier(ContainerBackgroundForWidgetModifier(background: background)) } } -struct DeviceRotationViewModifier: ViewModifier { - - let action: (UIDeviceOrientation) -> Void - - func body(content: Content) -> some View { - content - .onAppear() - .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in - action(UIDevice.current.orientation) - } - } -} - struct ContainerBackgroundForWidgetModifier: ViewModifier where Background: View { let background: () -> Background @@ -47,3 +30,27 @@ struct ContainerBackgroundForWidgetModifier: ViewModifier where Back } } } + +// MARK: - Only iOS + +#if os(iOS) +extension View { + + public func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View { + self.modifier(DeviceRotationViewModifier(action: action)) + } +} + +struct DeviceRotationViewModifier: ViewModifier { + + let action: (UIDeviceOrientation) -> Void + + func body(content: Content) -> some View { + content + .onAppear() + .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in + action(UIDevice.current.orientation) + } + } +} +#endif