Set the Size and Position of All Windows on the Screen in Swift

How to set the window size and position programmatically for a SpriteKit/GameScene app on OSX

It would be awesome if some UI guru could comment on whether this is the "right" answer, but here's what I did as a wild guess based on "El Tomato's" rather cryptic comments. First, add the following to ViewController.swift:

class WildGuessWindowController: NSWindowController {
override func windowDidLoad() {
if let screenSize = window?.screen?.frame {
window!.setFrame(screenSize, display: true)
print(screenSize)
}

super.windowDidLoad()
}
}

Next, open Main.storyboard in the project navigator. You might see something like the following:
Sample Image

Click on the box that says "Window Controller", and open the right-hand side panel. You should see something like this.

Sample Image

Notice the box next to the arrow, that has a grayed-out class name in it. (Also notice that you might have to click the "Identity inspector" button, the blue one above where it says "Custom Class".) Now click that drop-down, and you should see WildGuessWindowController. Select that, and you're set. Build and run. The framework will instantiate your class, and run windowDidLoad().

Swift -Position new UIWIndow in center of Screen

Change your center After setting the frame of the window will solve your issue ... hopefully it will help

// newWindow.center = endingCenter remove from here
newWindow.frame.size.width = endingWidth
newWindow.frame.size.height = endingHeight
newWindow.center = endingCenter // add it here

How do I set the minimum window size with swiftUI, and how do I make the window use the specified size and position when launching?

If you created project from template for macOS SwiftUI based, then all changes you need to do is in AppDelegate.swift.

The size of window is content-defined, so you need to specify root content view frame, and to disallow window position saving you need to remove setFrameAutosaveName, as a result your AppDelegate should look like the following

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

var window: NSWindow!

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
.frame(minWidth: 800, maxWidth: .infinity, minHeight: 500, maxHeight: .infinity)

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

Update: for SwiftUI life-cycle approach is the same - set frame to content view in window scene, like

var body: some Scene {
WindowGroup {
ContentView()
.frame(minWidth: 800, maxWidth: .infinity, minHeight: 500, maxHeight: .infinity)
}
}


Related Topics



Leave a reply



Submit