NSWindow位置

3
我正在以编程方式创建一个窗口,需要跨越两个屏幕。创建的窗口大小正确,但窗口从第一个屏幕中间开始。我可以将其拖回到第一个屏幕的起始位置,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!)
} 
1个回答

3
您正在设置内容矩形,但未设置框架。框架是窗口在屏幕坐标系下所占用的矩形。要移动窗口,您可以使用setFrameOrigin()setFrameTopLeftPoint()

内容rect也是屏幕坐标。 - Willeke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接