隐藏NSWindow标题栏

ind*_*gie 17 cocoa objective-c nswindow

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

任何帮助表示赞赏

小智 33

[yourWindow setStyleMask:NSBorderlessWindowMask];
Run Code Online (Sandbox Code Playgroud)

  • 我相信你还必须覆盖canBecomeKeyWindow或类似的东西.我尝试了这一切,所发生的一切都是我的窗户不显示.但是,我使用了bijan链接的Apple示例代码,并且工作得非常好. (4认同)

Eon*_*nil 15

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

window1.titlebarAppearsTransparent = true
window1.titleVisibility            = .Hidden
Run Code Online (Sandbox Code Playgroud)

也许你想要覆盖窗口样式.

window1.styleMask = NSResizableWindowMask
                  | NSTitledWindowMask
                  | NSFullSizeContentViewWindowMask
Run Code Online (Sandbox Code Playgroud)


Vla*_*lad 5

一种欢迎屏幕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)
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

结果

欢迎屏幕样式 NSWindow


Lyn*_*son 1

如果你获得关闭按钮的超级视图会发生什么?你能隐藏这一点吗?

// Imagine that 'self' is the NSWindow derived class
NSButton *miniaturizeButton = [self standardWindowButton:NSWindowMiniaturizeButton];
NSView* titleBarView = [miniaturizeButton superview];
[titleBarView setHidden:YES];
Run Code Online (Sandbox Code Playgroud)