1

I'm using a NSBorderlessWindowMask for my main window on a Swift project (without storyboards), when I load a Subview, the NSTextfield outlet is not keybard editable. I already put this code on the initialisation:

self.window?.makeKeyWindow()
self.window?.becomeKeyWindow()

this allows the outlet to be "blue" like on focus, but the keyboard editing is disabled, i can copy/paste on the textfield

PhiceDev
  • 507
  • 2
  • 6
  • 22

2 Answers2

2

You need to use a custom subclass of NSWindow and override canBecomeKeyWindow() to return true. By default, it returns false for windows without title bars (as documented).

You probably want to do the same for canBecomeMainWindow().

Also, never call becomeKeyWindow() (except to call super in an override). That is called by Cocoa to inform the window that it has become the key window. It does not instruct the window to become the key window.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
0

I found an awesome workaround for this problem: basically setup at beginning the NSWindow mask as NSTitledWindowMask, when application is loaded, remove set up the new mask NSBorderlessWindowMask

   func applicationWillFinishLaunching(notification: NSNotification) {
        self.window?.titleVisibility = NSWindowTitleVisibility.Hidden
        self.window?.styleMask = NSTitledWindowMask // adds title bar
    }

    func applicationDidFinishLaunching(aNotification: NSNotification) {

        self.window?.makeKeyWindow()
        self.window?.becomeKeyWindow()
        self.window.setIsVisible(true)
        self.window?.styleMask = NSBorderlessWindowMask // removes title bar
    }
PhiceDev
  • 507
  • 2
  • 6
  • 22