Adding secondary text to window title bar in Cocoa?

可紊 提交于 2019-12-23 07:50:03

问题


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:

.

Anyone have any suggestions?


回答1:


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];


来源:https://stackoverflow.com/questions/3833241/adding-secondary-text-to-window-title-bar-in-cocoa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!