objective-c 隐藏 NSWindow 标题栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2018147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:42:02  来源:igfitidea点击:

Hide NSWindow title bar

objective-ccocoanswindow

提问by indragie

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.

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

Any help is appreciated

任何帮助表示赞赏

回答by user257587

[yourWindow setStyleMask:NSBorderlessWindowMask];

回答by Eonil

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

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

window1.titlebarAppearsTransparent = true
window1.titleVisibility            = .Hidden

Maybe you want to override window style.

也许您想覆盖窗口样式。

window1.styleMask = NSResizableWindowMask
                  | NSTitledWindowMask
                  | NSFullSizeContentViewWindowMask

回答by Vlad

Kind of Welcome screenNSWindow / NSViewController setup (Swift 4.1)

一种欢迎屏幕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)
      }
   }
}

Result

结果

Welcome screen style NSWindow

Welcome screen style NSWindow

回答by Lyndsey Ferguson

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

回答by János

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).

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

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.

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

回答by nio

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.

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

回答by Dalmazio

You can use WAYInAppStoreWindowavailable on GitHub which works on Yosemite and Mavericks.

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