2012-03-31 130 views
7

過去幾天,我一直在絞盡腦汁想怎麼做一些看起來很簡單的事情。我已經四處搜尋,但找不到任何東西。將按鈕添加到窗口標題欄

我希望能夠在Mac上打開的所有窗口中添加一個按鈕(它將在右側,與「X」「 - 」「+」按鈕相對)。我相信我希望添加按鈕的區域被稱爲標題欄,儘管我不確定這一點。

我似乎無法在界面構建器中找到一種方法。但即使我這樣做了,我也希望按鈕出現在Mac上打開的所有窗口上(顯然一旦用戶安裝了該程序)。

據我所知,這實質上是「插入」操作系統的用戶界面,但我看到其他應用程序這樣做,這讓我覺得它是可行的。

只是尋找一些這方面的指針,我可以讀取它的地方,代碼片段和一般建議,我會採取任何事情。

下面是截圖,我希望按鈕:

Screenshot

+0

只要小心你添加它 - 用戶與Mac OS X 10.7及以上將有一個「全屏」按鈕在右側。確保你的按鈕有點向左移動。 – 2012-03-31 14:48:48

+0

一個有用的相關問題:http:// stackoverflow。com/questions/3833241/added-secondary-text-to-window-title-bar-in-cocoa – 2014-03-25 13:03:58

回答

6

這的確是一個問題兩個部分。至於如何讓一個按鈕在那裏,我建議使用-[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. 

封堵,在可能是最容易做的SIMBL插件。

+0

這可以在所有安裝的程序上運行嗎? – JoePasq 2012-03-31 13:38:09

+0

從我可以告訴,使用NSWindowCloseButton和類似的是用來決定是否顯示標準按鈕。 API在這裏不是很有用,不會告訴我該方法會返回什麼或什麼。 我的意思是,我不知道我將如何使用standardWindowButton:]來放置我自己的按鈕,它似乎是用於編輯現有的按鈕。 – Timsk 2012-03-31 13:57:21

+0

API返回請求的按鈕。你可以用它來隱藏它們,但你不需要。在該示例中,使用現有的窗口按鈕來查找包圍標準窗口按鈕的視圖(即,標題欄視圖)。我已經擴展了我的例子並添加了評論。我不太清楚你的意思是「不告訴我該方法會返回什麼或什麼」。 NSWindow類的引用[顯然是這樣](http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWindow_Class/Reference/Reference.html#//apple_ref/occ/instm/ NSWindow/standardWindowButton :)。 – gcbrueckmann 2012-03-31 14:11:38

7

官方支持的在OS X 10.10(Yosemite)及其後續版本中添加標題欄按鈕的方法是創建一個NSTitlebarAccessoryViewController並使用-[NSWindow addTitlebarAccessoryViewController]將其添加到您的窗口中。

例如,我有一個具有標題欄附件視圖窗口:

demo window title bar

要這樣設置,我開始在我的故事板將單獨的視圖窗口控制器的場景。 (您不必用故事板,但我在這個項目做了。)

accessory view

我的附件視圖是NSViewNSButton子視圖。按鈕標題使用Font Awesome來顯示圖釘。

我在NSWindowController子連接附件以出口(巧妙地命名爲accessoryView):

outlet connection

然後,在我的窗口控制器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) 
} 
相關問題