[英]How to avoid running window after its size scaled to the minimal size?
The main window has a minimal width, height and an aspect ratio via NSSize
However when the window scale to the minimal size, it would start to running downwards on the screen if the resizing event keep triggering from the top or right window corner.主窗口通过
NSSize
具有最小的宽度、高度和纵横比,但是当窗口缩放到最小尺寸时,如果调整大小事件从窗口右上角或右上角触发,它将开始在屏幕上向下运行。
Is there any window property have been missed here?这里是否遗漏了任何窗口属性?
let contentView = ContentView()
.frame(minWidth: 120, maxWidth: .infinity, minHeight: 50, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
/// Create the window and set the content view.
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 120, height: 50),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false, onKeyDown: handleKeyDownEvent)
window.contentView = NSHostingView(rootView: contentView)
window.contentView?.window?.aspectRatio = NSSize(
width: 120,
height: 50
)
Tried prevent the window from resizing via the window delegate method, windowWillResize(_:to:)
source but the window still running around when it get squeezed.尝试通过窗口委托方法
windowWillResize(_:to:)
源阻止窗口调整大小,但窗口在被挤压时仍在运行。
Also tried to use windowWillMove(_:)
source to print the window position and with an intension to stop it from moving.还尝试使用
windowWillMove(_:)
源来打印窗口位置并意图阻止它移动。 However, the position changed by squeezing the window wont be captured in the debug console.但是,通过挤压窗口更改的位置不会在调试控制台中捕获。
Remove消除
window.contentView?.window?.aspectRatio = NSSize(
width: 120,
height: 50
)
A better approach to make window resize in proportion is to do it inside windowWillResize(_:to:)
an NSWindowDelegate
method.使窗口按比例调整大小的更好方法是在
windowWillResize(_:to:)
一个NSWindowDelegate
方法中进行。 Don't forget to set the window.delegate
if you set up all user interface programmatically.如果您以编程方式设置所有用户界面,请不要忘记设置
window.delegate
。
extension AppDelegate: NSWindowDelegate {
func windowWillResize(_ sender: NSWindow, to frameSize: NSSize) -> NSSize {
return CGSize(width: frameSize.height * SOME_RATIO, height: frameSize.height)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.