How do I set the initial position of an app window for macOS?

I am trying to set the initial position of my app window. I have tried NSViewRepresentable and setFrameTopLeftPoint as shown below with no success. Is my syntax incorrect or do I need to utilize a different method?

struct WindowAccessor: NSViewRepresentable {
    @Binding var window: NSWindow?
    
    func makeNSView(context: Context) -> NSView {
        let view = NSView()
        view.window?.setFrameTopLeftPoint(CGPoint(x: 0, y: 0))
        DispatchQueue.main.async {
            self.window = view.window
        }
        return view
    }
    
    func updateNSView(_ nsView: NSView, context: Context) {}
}

and 

    var body: some Scene {
        WindowGroup {
            ContentView()
                .background(WindowAccessor(window: $window))
        }
    }

Found this tutorial at :

It is old but shows how to do what you want a few different ways.

"The origin for Cocoa views is the bottom left corner. So, Y values increase with distance from the bottom edge. This is in contrast to iOS where the view origin is at top-left."


let view = NSView()
view.window?.setFrameTopLeftPoint(CGPoint(x: 0, y: 0))

NSView is not automatically added in a window, so accessing window property will return nil and you won’t be setting any frame.

1 Like

Thank you

Terms of Service

Privacy Policy

Cookie Policy