21
votes

Is there a way to hide the titlebar in an NSWindow? I don't want to have to completely write a new custom window. I can't use NSBorderlessWindowMask because I have a bottom bar on my window, and using NSBorderlessWindowMask makes that disappear. I also tried using setContentBorderThickness:forEdge: with NSMaxYEdge and setting it to 0, that didn't work either.

Any help is appreciated

9
I pretty much just ended up changing the design of my app so that this isn't necessary, for all those who are thinking about doing the same, its not a great way to go.indragie

9 Answers

36
votes
[yourWindow setStyleMask:NSBorderlessWindowMask];
20
votes

Starting from OS X 10.10, you can hide title bar.

window1.titlebarAppearsTransparent = true
window1.titleVisibility            = .Hidden

Maybe you want to override window style.

window1.styleMask = NSResizableWindowMask
                  | NSTitledWindowMask
                  | NSFullSizeContentViewWindowMask
5
votes

Kind of Welcome screen NSWindow / NSViewController setup (Swift 4.1)

extension NSWindow {

   enum Style {
      case welcome
   }

   convenience init(contentRect: CGRect, style: Style) {
      switch style {
      case .welcome:
         let styleMask: NSWindow.StyleMask = [.closable, .titled, .fullSizeContentView]
         self.init(contentRect: contentRect, styleMask: styleMask, backing: .buffered, defer: true)
         titlebarAppearsTransparent = true
         titleVisibility = .hidden
         standardWindowButton(.zoomButton)?.isHidden = true
         standardWindowButton(.miniaturizeButton)?.isHidden = true
      }
   }
}

class WelcomeWindowController: NSWindowController {

   private (set) lazy var viewController = WelcomeViewController()
   private let contentWindow: NSWindow

   init() {
      contentWindow = NSWindow(contentRect: CGRect(x: 400, y: 200, width: 800, height: 472), style: .welcome)
      super.init(window: contentWindow)

      let frameSize = contentWindow.contentRect(forFrameRect: contentWindow.frame).size
      viewController.view.setFrameSize(frameSize)
      contentWindow.contentViewController = viewController
   }
}

class WelcomeViewController: NSViewController {

   private lazy var contentView = View()

   override func loadView() {
      view = contentView
   }

   init() {
      super.init(nibName: nil, bundle: nil)
   }

   override func viewDidLoad() {
      super.viewDidLoad()
      contentView.backgroundColor = .white
   }
}

class View: NSView {

   var backgroundColor: NSColor?

   convenience init() {
      self.init(frame: NSRect())
   }

   override func draw(_ dirtyRect: NSRect) {
      if let backgroundColor = backgroundColor {
         backgroundColor.setFill()
         dirtyRect.fill()
      } else {
         super.draw(dirtyRect)
      }
   }
}

Result

Welcome screen style NSWindow

1
votes

What happens if you get the superview of the close button? Can you hide that?

// Imagine that 'self' is the NSWindow derived class
NSButton *miniaturizeButton = [self standardWindowButton:NSWindowMiniaturizeButton];
NSView* titleBarView = [miniaturizeButton superview];
[titleBarView setHidden:YES];
1
votes

The only way I know would be to create a window without a titlebar (see NSBorderlessWindowMask). Note that you can't (easily) create a window without a titlebar in IB, so you will have to do a bit of work in code (there are a couple of different approaches, you can probably figure it out).

A big drawback with using a window without a titlebar is that you're now on the hook for much more of the standard appearance and behaviour - rounded corners and such.

1
votes

I had an experience that when I first set content view of my window and then set the window borderless:

[yourWindow setStyleMask:NSBorderlessWindowMask];

Nothing would appear in my window. So i first set the style mask and after that i've set the content view:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{ 
    // 1. borderless window
    [[self window] setStyleMask: NSBorderlessWindowMask];
    // 2. create the master View Controller
    self.masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
    // 3. Add the view controller to the Window's content view
    [self.window.contentView addSubview:self.masterViewController.view];
    self.masterViewController.view.frame = ((NSView*)self.window.contentView).bounds;     
}

And voila, the content of my window has appeared.

1
votes

Select Window in storyboard or XIB and tick the red circled option.

0
votes

You can use WAYInAppStoreWindow available on GitHub which works on Yosemite and Mavericks.

0
votes

Swift

NSApp.mainWindow?.styleMask = .borderless

enter image description here