
我想为Mac制作一个桌面应用程序,我已经设置了窗口级别,但它不工作。
发布于 2016-08-02 04:17:29
您需要检查窗口!=在任何自定义代码之前为零
class ViewController: NSViewController {
var addedObserver = false
override func viewDidLoad() {
super.viewDidLoad()
if let window = self.view.window {
// custom window here
window.level = Int(CGWindowLevelForKey(.FloatingWindowLevelKey))
} else {
addedObserver = true
self.addObserver(self, forKeyPath: "view.window", options: [.New, .Initial], context: nil)
}
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if let window = self.view.window {
// custom window here
window.level = Int(CGWindowLevelForKey(.FloatingWindowLevelKey))
}
}
deinit {
if addedObserver {
self.removeObserver(self, forKeyPath: "view.window")
}
}
}发布于 2019-09-15 18:16:28
让你的应用程序总是在Swift 5的其他应用程序之上是如此简单。
override func viewDidAppear() {
view.window?.level = .floating
}发布于 2018-03-02 17:44:57
还在测试中,但这是在Swift 4中的幼虫的答案,它似乎是有效的。
var observingFloat = false
override func viewDidLoad() {
super.viewDidLoad()
self.view.wantsLayer = true
if self.view.window != nil {
NSApplication.shared.windows.first?.level = NSWindow.Level(rawValue: 1)
} else {
observingFloat = true
self.addObserver(self, forKeyPath: "view.window", options: [.new, .initial], context: nil)
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if self.view.window != nil {
NSApplication.shared.windows.first?.level = NSWindow.Level(rawValue: 1)
}
}https://stackoverflow.com/questions/38711406
复制相似问题