Hide NSWindow title bar

后端 未结 8 1012
萌比男神i
萌比男神i 2020-12-13 00:46

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 botto

相关标签:
8条回答
  • 2020-12-13 01:43

    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

    0 讨论(0)
  • 2020-12-13 01:49

    Swift

    NSApp.mainWindow?.styleMask = .borderless
    

    0 讨论(0)
提交回复
热议问题