8

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?

Justin Boo
  • 10,132
  • 8
  • 50
  • 71
AengusMcMillin
  • 279
  • 2
  • 9
  • Possible duplicate of [Add button to top of windows (osx)](https://stackoverflow.com/questions/9955676/add-button-to-top-of-windows-osx) – rob mayoff Jul 21 '17 at 18:55

1 Answers1

10

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];
wbyoung
  • 22,383
  • 2
  • 34
  • 40
Sven
  • 22,475
  • 4
  • 52
  • 71