NSWindow位置

问题描述 投票:2回答:1

我正在以编程方式创建一个需要跨越2个屏幕的窗口。正在创建的窗口的大小是正确的,但是该窗口大约在第一个屏幕的一半处开始。我可以将其拖回到第一个屏幕的开头,NSWindow非常合适。

我只需要知道从窗口的起点来看我出了什么问题。

    func createNewWindow() {
    let bannerWidth = NSScreen.screens[0].frame.width * 2
    let bannerHeight = NSScreen.screens[0].frame.height
    let rect = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: bannerWidth, height: bannerHeight))
    let newWindow = NSWindow(contentRect: rect, styleMask: [.closable], backing: .buffered, defer: false)
    let storyboard = NSStoryboard(name: "Main", bundle: nil).instantiateController(withIdentifier: "ExternalScreen") as? NSViewController
    let window = storyboard?.view
    newWindow.styleMask.update(with: .resizable)

    NSMenu.setMenuBarVisible(false)
    newWindow.title = "New Window"
    newWindow.isOpaque = false
    newWindow.isMovableByWindowBackground = true
    newWindow.backgroundColor = NSColor(calibratedHue: 0, saturation: 1.0, brightness: 0, alpha: 0.7)
    newWindow.toggleFullScreen(true)
    newWindow.makeKeyAndOrderFront(nil)
    newWindow.toolbar?.isVisible = false
    newWindow.contentView?.addSubview(window!)
} 
swift macos nswindow
1个回答
2
投票

您正在设置内容矩形,但未设置框架。框架是窗口在屏幕坐标中占据的矩形。要移动窗口,可以单击setFrameOrigin()setFrameTopLeftPoint()

© www.soinside.com 2019 - 2024. All rights reserved.