Landscape vs Portrait

By Brendan

This post shows how to tell if you’re landscape or portrat.

GeometryReader { geometry in
     if geometry.size.height > geometry.size.width {
            print("portrait")
     } else {
            print("landscape")
     }

But Apple suggests that you don’t do that. They suggest you check if you’re .compact or .regular as per Paul’s suggestion:

struct ContentView: View {
    @Environment(\.horizontalSizeClass) var horizontalSizeClass

    var body: some View {
        if horizontalSizeClass == .compact {
            Text("Compact")
        } else {
            Text("Regular")
        }
    }
}

To tell if your device is landscape (rotated left) or landscape (rotated right), we probably need to import UIKit and then either detect the Device orientation…

if UIDevice.current.orientation == .portraitUpsideDown {
     print ("Device oriented vertically, home button on top")
}

… or maybe detect the User Interface orientation …

switch UIApplication.shared.statusBarOrientation {
    case .portrait:
      print("Home button in bottom")
    case .portraitUpsideDown
      print("Home button in top")
    case .landscapeLeft
      print("Home button in left")
    case .landscapeRight
      print("Home button in right")
    default:
      print("The interface may be rotating.")
    }

UI orientation is probably closer to what I really want than the device orientation … but UI orientation detection has been deprecated since iOS8.

See also this post for more.