我是 macOS 新手,有一个非常简单的项目,ViewController 中只有一个标签。在 WindowController 中,我尝试使用该代码设置窗口的大小:
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)")
}
}
}
日志显示:
screenRect (0.0, 30.0, 1680.0, 997.0)
windowFrame (0.0, 30.0, 840.0, 499.0)
但是,窗口不受影响,即无论我输入什么宽度/高度,它都保持不变。如果我用鼠标更改大小,下次打开它时,将完全是旧大小。
知道我在故事板或其他地方可能错过了什么吗?在我看来,我忘记了一些东西,因为它是如此基本......
(标签约束为上、左、右)