0

I would like to hide/close my main app window in func viewDidLoad() and only show/unhide the main window if some event requires it.

I tried self.view.window?.close() but this leaves a white window. I also tried NSApp.hide(nil) but then I can't unhide with NSApp.unhide(nil). Here is some sample code:

override func viewDidLoad() {
   super.viewDidLoad()
   // Do any additional setup after loading the view.
   NSApp.hide(nil)
   runTest()
}

func runTest () {
   let check = false

   if check == false {
      NSApp.unhide(nil)
   }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
doom4
  • 633
  • 5
  • 19
  • Uncheck "Visible At Launch" in IB if you want to window to be invisible by default. – Willeke Jan 20 '18 at 11:36
  • window.setIsVisible – El Tomato Jan 22 '18 at 06:58
  • Thanks for the suggestion El Tomato but is still doesent work. there must be a simple solution to: starting the app hidden and unhide if something happens without killing the window completely. – doom4 Jan 24 '18 at 05:47
  • Possible duplicate of [How to hide the initial window on start with OS X storyboards](https://stackoverflow.com/questions/27999177/how-to-hide-the-initial-window-on-start-with-os-x-storyboards) – Willeke Jan 25 '18 at 16:24

1 Answers1

2

From the NSWindow documentation

func orderOut(_ sender: Any?)

Removes the window from the screen list, which hides the window.

func makeKeyAndOrderFront(_ sender: Any?)

Moves the window to the front of the screen list, within its level, and makes it the key window; that is, it shows the window.

Hide and Close are two different things:

If the window is the key or main window, the window object immediately behind it is made key or main in its place. Calling orderOut(_:) causes the window to be removed from the screen, but does not cause it to be released. See the close() method for information on when a window is released. Calling orderOut(_:) on a child window causes the window to be removed from its parent window before being removed.

Hiding the application (NSApp.hide(nil) is another different thing: It

Hides all the receiver’s windows, and the next app in line is activated.

Community
  • 1
  • 1
vadian
  • 274,689
  • 30
  • 353
  • 361