Image rotation is automatic. So this applies if you want to change image when rotating.
Did you try adapting the proposed code:
struct ContentView: View {
@State private var orientation = UIDeviceOrientation.unknown
var body: some View {
Group {
if orientation.isPortrait {
Image("ImagePortrait")
} else if orientation.isLandscape {
Image("ImageLandscape")
} else if orientation.isFlat {
Image("ImageFlat")
} else {
Image("NoImage")
}
}
.onRotate { newOrientation in
orientation = newOrientation
}
// Continue view definition
}
Could you show the code where you define background image, so that we can see exactly how to fit.
You should probably define a computed var for the image:
struct ContentView: View {
@State private var orientation = UIDeviceOrientation.unknown
private var myImage : Image {
if orientation.isPortrait {
return Image("ImagePortrait")
} else if orientation.isLandscape {
return Image("ImageLandscape")
} else if orientation.isFlat {
return Image("ImageFlat")
} else {
return Image("NoImage")
}
}
var body: some View {
VStack{
Text("Hello")
}
onRotate { newOrientation in orientation = newOrientation }
.background(myImage)
}
}