22

有没有办法在 NSWindow 中隐藏标题栏?我不想完全编写一个新的自定义窗口。我不能使用 NSBorderlessWindowMask 因为我的窗口上有一个底栏,而使用 NSBorderlessWindowMask 会使它消失。我还尝试将 setContentBorderThickness:forEdge: 与 NSMaxYEdge 一起使用并将其设置为 0,但这也不起作用。

任何帮助表示赞赏

4

9 回答 9

36
[yourWindow setStyleMask:NSBorderlessWindowMask];
于 2010-01-23T22:56:56.467 回答
19

从 OS X 10.10 开始,您可以隐藏标题栏。

window1.titlebarAppearsTransparent = true
window1.titleVisibility            = .Hidden

也许您想覆盖窗口样式。

window1.styleMask = NSResizableWindowMask
                  | NSTitledWindowMask
                  | NSFullSizeContentViewWindowMask
于 2016-01-02T04:09:55.643 回答
6

欢迎屏幕的种类NSWindow / NSViewController 设置(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)
      }
   }
}

结果

欢迎屏幕样式 NSWindow

于 2018-01-29T22:34:03.260 回答
1

我知道的唯一方法是创建一个没有标题栏的窗口(请参阅 NSBorderlessWindowMask)。请注意,您不能(轻松)在 IB 中创建没有标题栏的窗口,因此您必须在代码中做一些工作(有几种不同的方法,您可能会弄清楚)。

使用没有标题栏的窗口的一个很大的缺点是,您现在需要更多的标准外观和行为——圆角等。

于 2010-02-11T14:23:56.457 回答
1

我有一个经验,当我第一次设置窗口的内容视图然后设置窗口无边框时:

[yourWindow setStyleMask:NSBorderlessWindowMask];

我的窗口中不会出现任何内容。所以我首先设置了样式掩码,然后我设置了内容视图:

- (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;     
}

瞧,我的窗口的内容已经出现了。

于 2013-07-30T08:02:01.617 回答
1

如果你得到关闭按钮的超级视图会发生什么?你能隐瞒吗?

// Imagine that 'self' is the NSWindow derived class
NSButton *miniaturizeButton = [self standardWindowButton:NSWindowMiniaturizeButton];
NSView* titleBarView = [miniaturizeButton superview];
[titleBarView setHidden:YES];
于 2010-01-08T19:37:48.787 回答
1

在 storyboard 或 XIB 中选择 Window 并勾选红色圈出的选项。

于 2021-01-12T14:16:46.120 回答
0

您可以使用GitHub 上可用的WAYInAppStoreWindow,它适用于 Yosemite 和 Mavericks。

于 2015-07-21T19:14:03.020 回答
-1

迅速

NSApp.mainWindow?.styleMask = .borderless

在此处输入图像描述

于 2020-07-22T14:36:23.210 回答