2

在我的 OS X 应用程序中,使用 Interface Builder,我有一个如下所示的窗口:

在此处输入图像描述

我想在右侧添加一个按钮,以实现此目的:

在此处输入图像描述

如果这是可能的,我该怎么做?

4

2 回答 2

6

使用 Interface Builder 是不可能的,但是您可以通过一点编码来完成它:

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.
于 2012-11-20T15:23:43.820 回答
0

Swift 2.2 和Auto Layout,在标题栏右侧创建一个“确定”按钮:

let myButton = NSButton()
myButton.title = "OK"
myButton.bezelStyle = .RoundedBezelStyle

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

使用自动布局,您无需对按钮的框架进行硬编码。即使您调整窗口大小,它也始终位于标题栏的右侧。

于 2016-08-07T05:45:37.033 回答