Keep window always on top?

自作多情 提交于 2019-11-27 11:34:09

To change the window level you can't do it inside viewDidload because view's window property will always be nil there but it can be done overriding viewDidAppear method or in a IBAction method:

Swift 1

override func viewDidAppear() {
    super.viewDidAppear()
    view.window?.level = Int(CGWindowLevelForKey(kCGFloatingWindowLevelKey))
}

Swift 2

view.window?.level = Int(CGWindowLevelForKey(.FloatingWindowLevelKey))

Swift 3

view.window?.level = Int(CGWindowLevelForKey(.floatingWindow))

Swift 4

Finally they fixed the odd syntax:

view.window?.level = .floating

I would prefer this way. This ignores all other active apps, and makes your app upfront.

    override func viewWillAppear() {            
        NSApplication.sharedApplication().activateIgnoringOtherApps(true)
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!