Hide NSWindow title bar

早过忘川 提交于 2019-11-28 17:18:42
user257587
[yourWindow setStyleMask:NSBorderlessWindowMask];

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

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];

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.

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!