Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

cocoa - How to change the height of an NSWindow titlebar?

I want to change the height of an NSWindow titlebar.

Here are some examples: alt text

And…

alt text

I could use an NSToolbar, but the problem is that I can't place views very height (For example: I can't place the segmentedControl higher than in the picture because there is still the titlebar) alt text

If I remove the titlebar I can't place a NSToolbar and the window isn't movable.

Have you any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is much easier than one would think. I too went on a quest to do something similar for my app.

Real App Store app: Here is the App Store app...

My App Store app look-alike: My App Store look-alike...

No disrespect to INAppStoreWindow, it is a very good implementation and solid. The only draw back I saw from it though was that there was a lot of drawing code along with hardcoded settings for the TitleBar colors which Apple can adjust at anytime.

So here is how I did it:

A) Create a standard window with a Title Bar, Close, Minimize, Shadow, Resize, Full Screen - Primary Window all set. Note: You do not need a textured window nor should you set a title

B) Next add a standard toolbar with these settings:

  • Icon Only
  • Visible at Launch - ON
  • Customizable - OFF
  • Separator - ON
  • Size - Regular

Remove all the Toolbar Items and add only these in the following order

NSSegmentControl (51 x 24) -- | Flexible Space | -- NSSearchField (150 x 25)

C) In your content View directly under the toolbar add a regular sized NSButton set like so:

  • Bordered - OFF
  • Transparent - OFF
  • Title -
  • Image -
  • Position - Text below the button
  • Font - System Small 11

Ok, pretty easy so far, right?!

In your Window Controller or app delegate.... setup IBOutlet(s) to your NSButton(s)

Note: Remember to hook up your IBOutlet in interface builder

Ok don't be scared we have to write a tiny bit of code now:

In awakeFromNib or windowDidLoad....

  1. Get the content views' superview (aka NSThemeView)
  2. Remove your button from its superView
  3. Set the frame of your button
  4. Add the button back to the theme view

So the code would look similar to this:

NSView *themeView = [self.contentView superview];
NSUInteger adj = 6;

[self.btnFeatured removeFromSuperview];
self.btnFeatured.frame = NSMakeRect( self.btnFeatured.frame.origin.x,
                              self.window.frame.size.height - self.btnFeatured.frame.size.height - adj,
                              self.btnFeatured.frame.size.width, self.btnFeatured.frame.size.height);
[themeView addSubview:self.btnFeatured];

That's it! You can use your outlet to enable/disable your button, setup a mask image when selected, enable/disable the toolbar or even hide everything and add a window title. All of this without worry if Apple changes their standard Window Titlebars.

P.S. No private frameworks were used in this posting whatsoever!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...