views:

26

answers:

2

I'm trying to create an NSSearchField programmatically; however, I don't know how to find the frame height of a standard NSSearchField (a la those in Interface Builder.) Obviously, I could simply copy the height from an NSSearchField entity in IB and call NSMakeFrame with that height, but that feels hackish - what happens when Apple changes the height of a typical search field? I'd rather the height not be hardcoded into my application. This is more of a "best practices" question, not an "I can't get it to work at all" one.

Thanks!

A: 

So you are subclassing the NSSearchField? you want to create it programmatically but you do not really make clear why. So you need the frame to do your custom drawing? I think when you are overriding the drawing code you should just try and see what works best, in other words try heights and see which one fits or suits most in your application design.

Antwan van Houdt
Nope, not subclassing. I'm merely instantiating it and placing it in an NSToolbar. I know that that can be done in IB, but I am hand-coding this particular window (more as an exercise than anything else.) I can call the following:NSSize searchFieldSize = NSMakeSize(400, 50);[searchField setFrameSize:searchFieldSize];but that feels kind of dirty to me.
Jake Zerrer
A: 

The magic method for this is

[mysearchfield sizeToFit]

which changes the frame to the minimum width and height. I use this in a Cocoa language wrapping where applications have to build their GUI completely without IB and it works on almost any NSControl class except for same cases (like small control size and NSComboBox) and for giving the width of icon only NSPopUpButton.

Also look at the Human Interface Guid of Apple. The pixel sizes are fixed so there is no danger in using magic pixel constants.

Lothar