Hello!
I found another possible solution. Let me know if it works for you:
import SwiftUI
import Combine
struct OrientationView: View {
@State private var orientation: UIDeviceOrientation? = nil
@State private var orientationChangePublisher: AnyCancellable?
var body: some View {
Text("Hello, World!")
.onAppear {
orientationChangePublisher = NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)
.compactMap { notification in
UIDevice.current.orientation
}
.sink { newOrientation in
orientation = newOrientation
print("isLandscape: \(orientation?.isLandscape ?? false))")
print("isPortrait: \(orientation?.isPortrait ?? false))")
print("isFlat: \(orientation?.isFlat ?? false))")
}
}
.onDisappear {
orientationChangePublisher?.cancel()
}
}
}
#Preview {
OrientationView()
}
Best regards.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: