xcode 将按钮添加到 Mac 窗口标题栏,系统范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9955676/
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-14 23:50:46  来源:igfitidea点击:

Add buttons to Mac window Title Bars, system-wide

xcodemacoscocoabutton

提问by Timsk

I want to be able to add a button to the title barof all windows that open on a Mac.

我希望能够在 Mac 上打开所有窗口标题栏中添加一个按钮。

The button will go on the right hand side, opposite the X - +buttons.

该按钮将位于右侧,与X - +按钮相对。

This is asked about windows of my apphere:
How can I create Yosemite-style unified toolbar in Interface Builder?

这是关于我的应用程序窗口的询问:
如何在界面生成器中创建优胜美地风格的统一工具栏?

But I want the button to appear on all windows, of any app, that are opened on the Mac. Obviously, this will only happen once the user has installed this program.

但我希望该按钮出现在Mac 上打开的任何应用程序的所有窗口上。显然,这只会发生在用户安装了这个程序之后。

I understand that this is essentially "plugging into" the OS's UI, but I have seen other apps do this, which makes me feel that it is do-able.

我知道这本质上是“插入”操作系统的用户界面,但我看到其他应用程序这样做,这让我觉得它是可行的。

Here is screenshot where I want the button:

这是我想要按钮的屏幕截图:

Screenshot

截屏

采纳答案by gcbrueckmann

This is really a two-part question. As for how to get a button up there, I'd suggest using -[NSWindow standardWindowButton:]to get an existing window button and its superview (i. e. the title bar):

这实际上是一个由两部分组成的问题。至于如何在那里获取按钮,我建议使用-[NSWindow standardWindowButton:]获取现有窗口按钮及其超级视图(即标题栏):

NSButton *closeButton = [window standardWindowButton:NSWindowCloseButton]; // Get the existing close button of the window. Check documentation for the other window buttons.
NSView *titleBarView = closeButton.superview; // Get the view that encloses that standard window buttons.
NSButton *myButton = …; // Create custom button to be added to the title bar.
myButton.frame = …; // Set the appropriate frame for your button. Use titleBarView.bounds to determine the bounding rect of the view that encloses the standard window buttons.
[titleBarView addSubview:myButton]; // Add the custom button to the title bar.

The plugging-in is probably easiest to do as a SIMBLplug-in.

作为SIMBL插件,该插件可能最容易实现。

回答by rob mayoff

The officially-supported way to add a title bar button in OS X 10.10 (Yosemite) and later is by creating an NSTitlebarAccessoryViewControllerand adding it to your window using -[NSWindow addTitlebarAccessoryViewController].

在 OS X 10.10 (Yosemite) 及更高版本中添加标题栏按钮的官方支持方法是创建一个NSTitlebarAccessoryViewController并使用-[NSWindow addTitlebarAccessoryViewController].

For example, I have a window that has a title bar accessory view:

例如,我有一个带有标题栏附件视图的窗口:

demo window title bar

演示窗口标题栏

To set this up, I started by adding a standalone view to the window controller scene in my storyboard. (You don't have to use a storyboard but I did in this project.)

为了进行设置,我首先向情节提要中的窗口控制器场景添加了一个独立视图。(您不必使用故事板,但我在这个项目中使用了。)

accessory view

附件视图

My accessory view is an NSViewwith an NSButtonsubview. The button title uses Font Awesome to display the pushpin.

我的附件视图是一个NSView带有NSButton子视图的视图。按钮标题使用 Font Awesome 来显示图钉。

I connected the accessory view to an outlet (cleverly named accessoryView) in my NSWindowControllersubclass:

我将附件视图连接到accessoryView我的NSWindowController子类中的插座(巧妙命名):

outlet connection

出口连接

Then, in my window controller's windowDidLoad, I create the NSTitlebarAccessoryViewController, set its properties, and add it to the window:

然后,在我的窗口控制器的 中windowDidLoad,我创建NSTitlebarAccessoryViewController,设置其属性,并将其添加到窗口中:

@IBOutlet var accessoryView: NSView!
var accessoryViewController: NSTitlebarAccessoryViewController?

override func windowDidLoad() {
    super.windowDidLoad()
    createAccessoryViewControllerIfNeeded()
}

fileprivate func createAccessoryViewControllerIfNeeded() {
    guard self.accessoryViewController == nil else { return }
    let accessoryViewController = NSTitlebarAccessoryViewController()
    self.accessoryViewController = accessoryViewController
    accessoryViewController.view = accessoryView
    accessoryViewController.layoutAttribute = .right
    self.window?.addTitlebarAccessoryViewController(accessoryViewController)
}

回答by frido

This answer addresses the latest Xcode Version 9.3

这个答案解决了最新的Xcode 版本 9.3

  • Go to a storyboard.
  • Drag and drop a toolbar in the storyboard to a Window.
  • 转到故事板。
  • 将故事板中的工具栏拖放到窗口中。

enter image description here

在此处输入图片说明

  • The toolbar will be visible below a title.
  • 工具栏将在标题下方可见。

enter image description here

在此处输入图片说明

  • Locate the Window on the storyboard
  • 在情节提要上找到窗口

enter image description here

在此处输入图片说明

  • Check "Hide Title" in properties of the Window.
  • 选中窗口属性中的“隐藏标题”。

enter image description here

在此处输入图片说明

  • The toolbar is a part of the title now.
  • 工具栏现在是标题的一部分。

enter image description here

在此处输入图片说明

回答by Iyyappan Ravi

Swift 4 - In NSWindowControlleradd the below code

Swift 4 -NSWindowController添加以下代码

if let window = window {
    let myButton = NSButton()
    myButton.title = "Help"
    myButton.bezelStyle = .rounded

    let titleBarView = window.standardWindowButton(.closeButton)!.superview!
    titleBarView.addSubview(myButton)
    myButton.translatesAutoresizingMaskIntoConstraints = false
    titleBarView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[myButton]-2-|", options: [], metrics: nil, views: ["myButton": myButton]))
    titleBarView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-1-[myButton]-3-|", options: [], metrics: nil, views: ["myButton": myButton]))
}

Hope this is helpful.

希望这是有帮助的。