0

I’m making an app, using Apple’s new framework SwiftUI, but I found a problem.

I want to get the height of user’s device. When I ‘m using UIScreen.main.bounds.height in the SwiftUI code, Xcode Preview will crash, but with no reason!? When I remove that line of code, the issue disappeared.

So, how do I get the user’s screen height, and use it in SwiftUI?

2
  • 3
    Why do you need the screen height? Are you trying lay things out? You shouldn't use the screen height directly when laying things out. Instead, use things like VStack, ZStack etc. Commented Feb 26, 2020 at 14:02
  • Irrelative to the needs it should not crash, or, actually, it does not crash - show your code. Commented Feb 26, 2020 at 14:35

2 Answers 2

2

You can get sizes in SwiftUI with GeometryReader. This could help with what you are doing, even it is not very well explained. But since there so many screen heights in different devices your layout should always be responsive and not fixed.

It is difficult, if not impossible, to get the size of a view. This is where the GeometryReader comes in. The GeometryReader is similar to a push-out container view in that you can add child views to. It will allow you to inspect and use properties that can help with positioning other views within it. You can access properties like height, width and safe area insets which can help you dynamically set the sizes of views within it so they look good on any size device. --> www.bigmountainstudio.com

    GeometryReader { geometry in
        VStack(spacing: 10) {
          Text("Width: \(geometry.size.width)")
          Text("Height: \(geometry.size.height)") }    
     }
Sign up to request clarification or add additional context in comments.

Comments

0

If you are working on multiplatform and in the Xcode selected device as (My Mac) then using UIScreen.main.bounds.height will cause an error. So, to avoid this error and get the device screen height or width for multiplatform in swiftui, you can write this class:

class Device {
    static var screen = Device()
    #if os(watchOS)
    var width: CGFloat = WKInterfaceDevice.current().screenBounds.size.width
    var height: CGFloat = WKInterfaceDevice.current().screenBounds.size.height
    #elseif os(iOS)
    var width: CGFloat = UIScreen.main.bounds.size.width
    var height: CGFloat = UIScreen.main.bounds.size.height
    #elseif os(macOS)
    // You could implement this to force a CGFloat and get the full device screen size width regardless of the window size with .frame.size.width
    var width: CGFloat? = NSScreen.main?.visibleFrame.size.width
    var height: CGFloat? = NSScreen.main?.visibleFrame.size.height
    #endif
}

and use it like this:

let screenWidth = Device.screen.width
let screenHeight = Device.screen.height

Comments

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.