SwiftUI: How do I detect a change in device orientation?

Please can someone help I have spent 3 - 4 days trawling the internet and only finding complex answers that I can't get to work, and my head is ready to explode.

I only want to detect when the device orientation has changed and load a different background image. This is what I have so far:

import SwiftUI

struct Main_Menu_iPhone: View {
    @State private var bloodGlucose: String = "0.0"
    var body: some View {
        ZStack {
            Image("iPhone Background Portrait 828 x 1792 ")
                .resizable()
                .aspectRatio(contentMode: .fill)

           VStack {
               TextField("Blood Glucose : ", text: $bloodGlucose)
               Spacer()
           }
        }
        
    }
    
}

#Preview {
    Main_Menu_iPhone()
}
Answered by DTS Engineer in 788487022

@jamesm46 Your question has been answered in a different forums post, please have a look at that thread.

@jamesm46 Your question has been answered in a different forums post, please have a look at that thread.

Several simple solutions here: https://forums.developer.apple.com/forums/thread/126878

Did you see it ?

But note eskimo's post to strongly discourage to use landscape or portrait information in SwiftUI…

SwiftUI: How do I detect a change in device orientation?
 
 
Q