25

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

indragie
  • 18,002
  • 16
  • 95
  • 164
  • 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 Feb 17 '10 at 01:00

9 Answers9

36
[yourWindow setStyleMask:NSBorderlessWindowMask];
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
user257587
  • 393
  • 2
  • 2
23

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
eonil
  • 83,476
  • 81
  • 317
  • 516
6

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

Vlad
  • 6,402
  • 1
  • 60
  • 74
1

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.

nio
  • 5,141
  • 2
  • 24
  • 35
1

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];
Lyndsey Ferguson
  • 5,306
  • 1
  • 29
  • 46
  • 1
    I tried this and it just turns the window white and blank. If I'm not mistaken, the superview of the button is NSThemeFrame. However, I might try adjusting the frame of the view to see if I can shrink it. – indragie Jan 08 '10 at 23:31
  • Adjusting the frame doesn't work well, I get all sorts of strange results. Pretty much stuck I suppose. – indragie Jan 09 '10 at 00:28
  • The reason that didn't work is that the NSThemeFrame is the superview of the window's contentView. Hide the frame view, and all its subviews get hidden, too. – NSResponder Jan 09 '10 at 01:07
  • Just for fun I class dumped NSThemeFrame's header and other related classes then found some private methods in there for returning the titlebar height. Then I loaded my custom theme frame subclass in my NSWindow subclass and tried it, and it did work, but it got rid of my rounded corners and the bottom bar as well. Oh well. – indragie Jan 21 '10 at 03:02
1

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.

János
  • 32,867
  • 38
  • 193
  • 353
1

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

Merkurial
  • 242
  • 3
  • 17
0

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

Dalmazio
  • 1,835
  • 2
  • 23
  • 40
-1

Swift

NSApp.mainWindow?.styleMask = .borderless

enter image description here

NamNamNam
  • 1,190
  • 3
  • 13
  • 22