6

I just create a new application project

  • Xcode Version 7.3 (7D175)
  • Swift Version 2.2
  • Mac OS version OS X El Capitan 10.11.4

I want to make a desktop app for Mac OS and I had set the window level but it's not work.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Steve Jiang
  • 653
  • 7
  • 17

3 Answers3

14

Making your app always on top of the other applications in Swift 5 is so simple.

override func viewDidAppear() {
    view.window?.level = .floating
}
Esmaeil MIRZAEE
  • 1,110
  • 11
  • 14
4

You need check window != nil before any custom code

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")
        }
    }
}
larva
  • 4,687
  • 1
  • 24
  • 44
  • Thank you. I had add your code to my project , but it's can't keep window on the top – Steve Jiang Aug 02 '16 at 04:40
  • @SteveJiang did code in // custom window here get called? I did create a new project and try this code. It work and bring window to font top, but I'm not sure what NSWindow level do. – larva Aug 02 '16 at 04:51
  • Oh my god... It's weird . I create an another new project ,and run your code .It's work well but It's not work in my old project , the code is same. – Steve Jiang Aug 02 '16 at 05:26
1

Still testing it, but this is Larva's answer in Swift 4 and it seems to be working.

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)
        }
    }
Dave Levy
  • 1,036
  • 13
  • 20