0

Using iOS14.4.2, Swfit5.3.2, XCode12.4, physical device = iPhoneXS

On appearing, I am trying to write the current device orientation into a Static property and later use it inside a View.

However, the value is not correctly written when appearing.

Here is my code (see below)

Note that isPortrait will always be false - no matter how I orient my iPhone at startup. Especially, when the iPhone is in Portrait at startup, shouldn't the property be true ??

Why this strange behaviour ?

Apple should intensify device-orientation issues with SwiftUI, I think.

struct MyView: View {
  
    @State var isPortrait: Bool = true
 
    var body: some View {
    
        VStack {
            Text("text1")
            if isPortrait {
                Test("text2")      // never shown why ???
            }
        }
        .onAppear {
            isPortrait = (UIDevice.current.orientation == .portrait)
            print(isPortrait)  // always prints 'false' why ????
        }
    } 
}

And please, don't tell me to use GeometryReader instead since this does not work on iPad (yet).

iKK
  • 6,394
  • 10
  • 58
  • 131

1 Answers1

1

Use this one:

UIApplication.shared.windows.first?.windowScene?.interfaceOrientation.isPortrait
Kuhlemann
  • 3,066
  • 3
  • 14
  • 41
  • Thank you - this works inside `onAppear`. It is good to have a replacement - but why would the first one not work in the first place ? – iKK Apr 05 '21 at 10:58
  • I didn't find a reason for it, but it has nothing to do with your code. Most likely a bug. – Kuhlemann Apr 05 '21 at 11:42