0

i'm playing around with SwiftUI and try to get correct screen size depending on orientation. The solution works fine in simulator on iPhone but lacks on iPad (i have no iPad for testing real). It seems on iPad it is just missing the redraw-event. Is there a bug in SwiftUI or did I need to add some code?

struct ContentView: View {
    @Environment(\.layoutDirection) var layoutDir
    // verticalSizeClass initiate redrawing UI based on new direction (horSizeClass won't work)
    @Environment(\.verticalSizeClass) var vertSizeClass

    var body: some View {
        VStack{
            Text("UIDevice.current.orientation").font(.title)
            if UIDevice.current.orientation.isPortrait {
                Text("Portrait")
            } else if UIDevice.current.orientation.isLandscape {
                Text("Landscape")
            } else if UIDevice.current.orientation.isFlat {
                Text("is Flat")
            }
            Text("UIScreen.main.bounds").font(.title)
            Text("Width:\(UIScreen.main.bounds.width)")
            Text("Height:\(UIScreen.main.bounds.height)")
            Text("@Environment(\\.horizontalSizeClass").font(.title)
            if  vertSizeClass == .regular {
                Text("regular")
            } else if vertSizeClass == .compact {
                Text("compact")
            } else {
                Text("default")
            }
        }
    }
}
3
  • 1
    I answered your question, but someone deleted it because it had a link to the solution. Commented Apr 20, 2020 at 5:25
  • Thanks for your effort. Can you please post the link here again? Commented Apr 20, 2020 at 7:07
  • 2
    copy it quickly before the @$%&* deletes it again. forums.developer.apple.com/thread/126878 Commented Apr 20, 2020 at 7:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.