Controlling window size on Mac

Hi,

I am trying to write my first Mac app with SwiftUI. I have everything working, but when the app comes up, it only shows half of the app. I have to manually size the window each time the app is run.


How do I adjust the size so the entire app can be viewed at one time?


Thanks,

Dan Uff

There should be a way to adapt the size in AppDelegate:


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        let contentView = ContentView()

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
          // SET SIZE HERE
            window.makeKeyAndVisible()
        }
    }

Thanks for the info. It didnt work. This is the only thing holding the app back from being released.


I did try adjusting the NSRect in the AppDelegate:


// Create the window and set the content view. 
    window = NSWindow(
        contentRect: NSRect(x: 0, y: 0, width: 230, height: 400),
        styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
        backing: .buffered, defer: false)
    window.center()
    window.setFrameAutosaveName("Main Window")
    window.contentView = NSHostingView(rootView: contentView)
    window.makeKeyAndOrderFront(nil)
  }


The Width and Height, but that doesnt seem to do anything. Also, where does one get information for the X and Y coordinates? Can't find information about that either.

What do you get exactly (as window size and position) ?

What did you expect ?


What is the question for X and Y ?

contentRect

Origin and size of the window’s content area in screen coordinates. Note that the window server limits window position coordinates to ±16,000 and sizes to 10,000.

It sounds like your window size is too small for your content. Did you get a 230x400 size window?


The (0,0) is the position of the window's lower left corner in screen coordinates. If you don't call window.center(), you see your window appear in the lower left hand corner. The window.center() call causes the default NSWIndowController to place the screen in the center.


Might try something like this:


window = NSWindow(...)
window.contentView = NSHostingView(rootView: contentView
<figure out what the frame size should be>
window.setFrame(NSRect(x:0.0, y: 0.0, width: <best width, height: <best height>), display: true)
window.setFrameAutosaveName(...)
window.center()
window.makeKeyandShow()

Using the setFrame call resets the frame and width display: true, causes the view to recalculate it's size.

HI again,

I expect that th window would show the correct size and auto adjust for the app (again, I am VERY new at developing for Mac) Like it used to be way back when.


I wish I could show you (via a screen shot) what I am trying to accomplish. Basically, the app's window is almost closed. A user would have to manually resize the window in order to see the entire app. The app would appeear as a rectangle with one label (showing the name of the app), and and two text fields on the side of the label (via a HStack) for the user to input two integers, kind of like what you see with some random number generator apps for Mac. So, it would be like:


name of app. Field one. Field two.


But, it first appears as:


name of Field.... Field....


Thus having to manually resize the window.


Thanks,

Dan

Thanks for the info.

Hi I'm having the same issue but I managed to have solved it. I've set the window's minSize variable like this in AppDelegate.swift:


        window = NSWindow(
            contentRect: NSRect(x: 0, y: 0, width: 916, height: 648),
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered, defer: false)
        window.center()
        window.aspectRatio = NSSize(width: 229, height: 162)
        window.minSize = NSSize(width: 916, height: 648)
        window.setFrameAutosaveName("Main Window")
        window.contentView = NSHostingView(rootView: contentView)
        window.makeKeyAndOrderFront(nil)


That prevents the window from opening with a smaller size, but it doesn't prevent the user from resizing the window smaller than that.

Controlling window size on Mac
 
 
Q