【问题标题】:Hide NSWindow title bar隐藏 NSWindow 标题栏
【发布时间】:2010-01-07 04:07:04
【问题描述】:

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

感谢任何帮助

【问题讨论】:

标签: objective-c cocoa nswindow


【解决方案1】:
[yourWindow setStyleMask:NSBorderlessWindowMask];

【讨论】:

  • 我相信您还必须覆盖 canBecomeKeyWindow 或类似的东西。我试过这个,所发生的只是我的窗口不显示。但是,我使用了 bijan 链接的 Apple 示例代码,效果很好。
  • indragie 明确表示他不能使用 NSBorderlessWindowMask。
  • @KennyWyland 链接是什么?
  • 这对我 atm 不起作用......即使我覆盖“canBecomeKeyWindow”等。
【解决方案2】:

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

window1.titlebarAppearsTransparent = true
window1.titleVisibility            = .Hidden

也许你想覆盖窗口样式。

window1.styleMask = NSResizableWindowMask
                  | NSTitledWindowMask
                  | NSFullSizeContentViewWindowMask

【讨论】:

  • 为 Swift 5 更新:window.styleMask = [.resizable, .titled, .closable, .miniaturizable, .fullSizeContentView]
  • 这在全屏模式下不起作用。当您将鼠标定位到屏幕顶部时 - 仍会显示标题栏
【解决方案3】:

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

结果

【讨论】:

【解决方案4】:

如果你获得关闭按钮的超级视图会发生什么?可以隐藏吗?

// Imagine that 'self' is the NSWindow derived class
NSButton *miniaturizeButton = [self standardWindowButton:NSWindowMiniaturizeButton];
NSView* titleBarView = [miniaturizeButton superview];
[titleBarView setHidden:YES];

【讨论】:

  • 我试过这个,它只是把窗口变成白色和空白。如果我没记错的话,按钮的superview是NSThemeFrame。不过,我可能会尝试调整视图的框架,看看是否可以缩小它。
  • 调整框架效果不好,我得到了各种奇怪的结果。我想几乎是卡住了。
  • 不起作用的原因是 NSThemeFrame 是窗口 contentView 的父视图。隐藏框架视图,它的所有子视图也会被隐藏。
  • 只是为了好玩,我在课堂上转储了 NSThemeFrame 的标题和其他相关类,然后在其中找到了一些用于返回标题栏高度的私有方法。然后我在我的 NSWindow 子类中加载了我的自定义主题框架子类并尝试了它,它确实有效,但它也摆脱了我的圆角和底栏。哦,好吧。
【解决方案5】:

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

使用不带标题栏的窗口的一大缺点是您现在处于 钩住更多标准外观和行为 - 圆角 等等。

【讨论】:

    【解决方案6】:

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

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

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

    【讨论】:

      【解决方案7】:

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

      【讨论】:

        【解决方案8】:

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

        【讨论】:

          【解决方案9】:

          斯威夫特

          NSApp.mainWindow?.styleMask = .borderless
          

          【讨论】:

          • 你不能在运行时改变这个东西。这会使您的应用崩溃。
          猜你喜欢
          • 2015-04-06
          • 2015-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-31
          相关资源
          最近更新 更多