Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

457
Vistas
Show and hide window instead of terminating app on close click in cocoa app

I have been trying to show hide window on close(red button) click on the window. I would like to do is hide the window and when a user clicks on my app again it will show again.

Thanks in advance to all the developers who provide an answer. I am new to Cocoa apps. I am iOS developers so I have not much knowledge about cocoa apps.

I tried to hide(:) method and orderOut(:) method too. but not working.

class ViewController : NSViewController, NSWindowDelegates {

    override func viewDidAppear() {
         self.view.window?.delegate = self
    }

    func windowShouldClose(_ sender: NSWindow) -> Bool {
         //NSApplication.shared.terminate(self)
         //NSApp.hide(self)
         //self.view.window?.orderOut(sender)
        return false
    }
}

I want to create a timer app which runs in the background if user click on close it will hide instead of terminating. and when he clicks again from dock menu it will reopen the window.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I'm not much into Mac OS development, but I think you should inherit from NSWindowController like this:

class MyWindowController: NSWindowController, NSWindowDelegate {

    func windowShouldClose(_ sender: NSWindow) -> Bool {
        NSApp.hide(nil)
        return false
    }
}

Then you need just open your Main (or whatever name you have) storyboard, choose Window Controller and set your MyWindowController to it:

enter image description here

I tried and it worked for me.

over 4 years ago · Santiago Trujillo Denunciar

0

I have found solution. Thanks to @Silvester suggestion to present NSViewController. On button click event :

@IBAction func onButtonClick(_ sender: VSButton) {
    let animator = ReplacePresentationAnimator()
    let vc = self.storyboard?.instantiateController(withIdentifier: "identifier") as! yourVC
    present(vc, animator: animator) 
}

Custom animator class :

  class ReplacePresentationAnimator: NSObject, NSViewControllerPresentationAnimator {

        func animatePresentation(of viewController: NSViewController, from fromViewController: NSViewController) {
            if let window = fromViewController.view.window {
                NSAnimationContext.runAnimationGroup({ (context) -> Void in
                fromViewController.view.animator().alphaValue = 0
            }, completionHandler: { () -> Void in
                viewController.view.alphaValue = 0
                window.contentViewController = viewController
                viewController.view.animator().alphaValue = 1.0
                })
            }
        }

        func animateDismissal(of viewController: NSViewController, from fromViewController: NSViewController) {
            if let window = viewController.view.window {
                NSAnimationContext.runAnimationGroup({ (context) -> Void in
                viewController.view.animator().alphaValue = 0
            }, completionHandler: { () -> Void in
                fromViewController.view.alphaValue = 0
                window.contentViewController = fromViewController
                fromViewController.view.animator().alphaValue = 1.0
                })
            }
        }
    }

This will work perfectly with Silvester MyWindowController. Thank you.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda