Home > OS >  How can I access the height of a Mac app's main window using SwiftUI?
How can I access the height of a Mac app's main window using SwiftUI?

Time:12-24

I've been following a tutorial to learn SwiftUI and build an app for my Mac. The tutorial has been assuming that I'm writing for an iPhone, which hasn't been a problem until just now.

I need to access the height of the screen, or in my case the window. The tutorial is using UIScreen.main.bounds.height to do so, but since UIScreen isn't available for Mac apps, I'm trying to find the Mac equivalent. I'm pretty sure that the window that I'm trying to access is of type NSWindow, but I don't see any ways of accessing width and height of an NSWindow in the documentation. On top of that, I don't know how I could even go about accessing the NSWindow since there I don't see a variable or anything holding that information.

enter image description here

CodePudding user response:

Apart from GeometricReader, you can also use UIScreen;

GeometryReader { geo in
   //Your code here
}
.frame(width: geo.size.width, height: geo.size.height)

Or 

.frame(width: UIScreen.main.bounds.width, height: 
 UIScreen.main.bounds.height)
 
  • Related