I'm new to macOS and have a very simple project with just one label in the ViewController. In the WindowController I'm trying to set the size of the window with that code:
import Cocoa
class WindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
if let window = window, let screen = NSScreen.main {
let screenRect = screen.visibleFrame
print("screenRect \(screenRect)")
window.setFrame(NSRect(x: screenRect.origin.x, y: screenRect.origin.y, width: screenRect.width/2.0, height: screenRect.height/2.0), display: true, animate: true)
print("windowFrame \(window.frame)")
}
}
}
The log shows:
screenRect (0.0, 30.0, 1680.0, 997.0)
windowFrame (0.0, 30.0, 840.0, 499.0)
However, the window isn't affected, i.e. whatever I enter as width/height, it stay the same. If I change the size with the mouse, next time I open it, exactly the old size.
Any idea what I may missed in storyboard or anywhere else? Seems to me that I forgot something as it is so basic..... (The label is constraint to the top, left, right)
... did load...
note: on didLoad without a small delay it does not work. (nil refs...)
Found it, thanks to PUTTIN Why NSWindow animator setFrame:display:animate: didn't work sometimes?
The magic thing is, to have it on the mainThread
Now it works!