我在特定螢屏(即主顯示幕而不是外部顯示幕)上顯示我的應用程式視窗時遇到了困難。我想在主顯示幕上顯示我的應用程式的原因是我想稍后使用CGDisplayCapture(CGDirectDisplayID(2)).
我發現了以下幾個建議和實作。
Swift 3/macOS:在特定螢屏上打開視窗
如何更改出現 NSWindow 的 NSScreen
在 os x 上的多個顯示幕上顯示視窗
如何將 NSWindow 移動到特定螢屏?
但是,我無法弄清楚如何在Swift 5. 這可能是因為以前的帖子已經過時了。例如,以下我的代碼(基于Swift 3/macOS:在特定螢屏上打開視窗)不起作用......它沒有做任何事情。
override func viewDidLoad() {
super.viewDidLoad()
//set up the main display as the display where window shows up
let screens = NSScreen.screens
var pos = NSPoint()
pos.x = screens[0].visibleFrame.midX
pos.y = screens[0].visibleFrame.midY
self.view.window?.setFrameOrigin(pos)
如果我能聽到如何實作這一點,我將不勝感激。
補充:提供給Cocoa 在特定螢屏上顯示 NSWindow的答案似乎是用 Objective-c 撰寫的。不幸的是,我不了解objective-c。
uj5u.com熱心網友回復:
這ViewController不是做這件事的正確地方。使用 aWindowController并將相關代碼放入 中windowDidLoad,或者,如果您確實想要一個導致其父視窗填充主顯示viewDidMoveToWindow的視圖,則覆寫您的視圖。(在 IMO 的大多數情況下,后者會有點奇怪。)
uj5u.com熱心網友回復:
感謝@Willeke 提出的問題,我能夠使用以下代碼在主螢屏上顯示視窗(而不是外部顯示幕)。我只需要使用DispatchQueue.main.async {}.
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
//set up the main display as the display where window shows up
let screens = NSScreen.screens
var pos = NSPoint()
pos.x = screens[0].visibleFrame.midX
pos.y = screens[0].visibleFrame.midY
self.view.window?.setFrameOrigin(pos)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/375677.html
