简体   繁体   English

如何避免在其大小缩放到最小大小后运行窗口?

[英]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.

相关问题 如何在不改变其位置的情况下更改窗口框架大小 - How to change window frame size without change its location UIFontMetrics:缩放字体大小计算 - UIFontMetrics: scaled font size calculation 在Swift中的HeightForRowAt函数中设置UITableViewCell的大小后如何调整其大小? - How to resize UITableViewCell after setting its size in HeightForRowAt func in Swift? GeometryReader 似乎更改了使用其大小数据的视图布局。 我怎样才能避免这种情况? - GeometryReader seems to change layout of view that is using its size data. How can I avoid this? 如何减小模拟器窗口的大小? - How to reduce the size of the emulator window? 对齐时如何避免图像尺寸缩小 - How to avoid Image size shrink when aligning 根据其内容SWIFT设置高度后,UITextView的大小 - Size of UITextView after setting its height depending of its content SWIFT 如何根据UILabel的字体大小更改其大小? - How do I change UILabel size depending on its font size? 如何将Modal Popup的大小更改为等于其Superview的大小? - How to change Modal Popup size to equal to its superview size? iOS Swift-在iPhone 5s中运行时窗口大小会缩小 - IOS Swift - Window size shrinks when running in iphone 5s
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM