Skip to content

Instantly share code, notes, and snippets.

@bballentine
Created August 26, 2024 16:04
Show Gist options
  • Save bballentine/8db5ca95e4f238753e8fcf2b143a72a4 to your computer and use it in GitHub Desktop.
Save bballentine/8db5ca95e4f238753e8fcf2b143a72a4 to your computer and use it in GitHub Desktop.
SwiftUI Device Rotation View Modifier
import SwiftUI
struct DeviceRotationViewModifier: ViewModifier {
let action: (UIDeviceOrientation) -> Void
func body(content: Content) -> some View {
content
.onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
action(UIDevice.current.orientation)
}
}
}
extension View {
func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View {
self.modifier(DeviceRotationViewModifier(action: action))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment