Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding secondary text to window title bar in Cocoa?

Tags:

cocoa

titlebar

I was hoping to release my software with a trial period and I was wondering how I could show a link in the right side of my title bar telling them how long their trial will last similar to this:

What Coda Does.

Anyone have any suggestions?

like image 628
AengusMcMillin Avatar asked Sep 30 '10 17:09

AengusMcMillin


1 Answers

You can get the superview of the windows content view and add a custom view to that. Just make sure you position your view correctly. Here is some sample code:

NSView *frameView = [[window contentView] superview];
NSRect frame = [frameView frame];

NSRect otherFrame = [otherView frame];
otherFrame.origin.x = NSMaxX( frame ) - NSWidth( otherFrame );
otherFrame.origin.y = NSMaxY( frame ) - NSHeight( otherFrame );
[otherView setFrame: otherFrame];

[frameView addSubview: otherView];

Here otherView is the view you want to put in your title bar. This code won’t work though if there is a toolbar button - they would overlap. Luckily there is an API to get the toolbar button so you can calculate the position:

NSButton *toolbarButton = [window standardWindowButton: NSWindowToolbarButton];
otherFrame.origin.x = NSMinX( [toolbarButton frame] ) - NSWidth( otherFrame );

You also have to make sure that the autosizing masks for your view are set up so that it stays in the upper right corner of the window:

[otherView setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
like image 131
Sven Avatar answered Nov 13 '22 06:11

Sven