以编程方式设置 NSWindow 大小不起作用
Set NSWindow Size programmatically doesn't work
我是 macOS 新手,有一个非常简单的项目,在 ViewController 中只有一个标签。在 WindowController 中,我尝试使用该代码设置 window 的大小:
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)
然而,window 不受影响,即无论我输入什么 width/height,它都保持不变。如果我用鼠标改变尺寸,下次我打开它,完全是旧尺寸。
知道我在故事板或其他任何地方可能遗漏了什么吗?在我看来,我忘记了一些东西,因为它是如此基本......
(标签约束在上、左、右)
找到了,感谢 PUTTIN Why NSWindow animator setFrame:display:animate: didn't work sometimes?
神奇的是,把它放在主线程上
DispatchQueue.main.async {
window.setFrame(NSRect(x: screenRect.origin.x, y: screenRect.origin.y, width: screenRect.width/1.0, height: screenRect.height/1.0), display: true, animate: true)
print("windowFrame \(window.frame)")
}
}
现在可以了!
...加载...
// decide if needed..
let when = DispatchTime.now() + 0.5
DispatchQueue.main.asyncAfter(deadline: when, execute: { [weak self] in
self?.setSize()
})
} //setupUI
private final func setSize(){
if let w = self.view.window{
var frame = w.frame
frame.size = NSSize(width: 800, height: 600)
w.setFrame(frame, display: true, animate: true)
}
}
注意:在 didLoad 上没有一点延迟它就不起作用。 (无参考...)
我是 macOS 新手,有一个非常简单的项目,在 ViewController 中只有一个标签。在 WindowController 中,我尝试使用该代码设置 window 的大小:
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)
然而,window 不受影响,即无论我输入什么 width/height,它都保持不变。如果我用鼠标改变尺寸,下次我打开它,完全是旧尺寸。
知道我在故事板或其他任何地方可能遗漏了什么吗?在我看来,我忘记了一些东西,因为它是如此基本...... (标签约束在上、左、右)
找到了,感谢 PUTTIN Why NSWindow animator setFrame:display:animate: didn't work sometimes?
神奇的是,把它放在主线程上
DispatchQueue.main.async {
window.setFrame(NSRect(x: screenRect.origin.x, y: screenRect.origin.y, width: screenRect.width/1.0, height: screenRect.height/1.0), display: true, animate: true)
print("windowFrame \(window.frame)")
}
}
现在可以了!
...加载...
// decide if needed..
let when = DispatchTime.now() + 0.5
DispatchQueue.main.asyncAfter(deadline: when, execute: { [weak self] in
self?.setSize()
})
} //setupUI
private final func setSize(){
if let w = self.view.window{
var frame = w.frame
frame.size = NSSize(width: 800, height: 600)
w.setFrame(frame, display: true, animate: true)
}
}
注意:在 didLoad 上没有一点延迟它就不起作用。 (无参考...)