0

Originally, I wanted to ask how to create user interfaces with cocoa programmatically, i.e. without using interface builder. But it seems that someone else has already asked this question and the answers didn't help me.

So I'll ask a different question, that I hope will indirectly help me answer the previous question. Here it is:

(QUESTION_START)

How do I create an Objective C class that is equivalent in functionality with the BoxLayout class in Java? (Just click the link, the image on that page says everything you need to know about BoxLayout.)

(QUESTION_END)

Any help in the right direction will be appreciated!

There are a few sub tasks that are connected with the question, e.g.

"How do I ask a user interface element (e.g. a button) how large it wants to be" (before it has been drawn to the screen). To draw it on the screen you have to already know its size, don't you? Obviously, the interface builder application has figured out a way to do this.

I know that many Cocoa developers think it's a stupid idea to even try what I want to do. Let me tell you: I know the reasoning behind that opinion. Right now, laying out controls without interface builder sucks, because there is nothing that comes even close to a layout manager in cocoa. But if you think my question is stupid, please DONT answer. The whole internet is full of explanations why you would never want to create UIs with code in cocoa.

Thanks!

Community
  • 1
  • 1
Michael
  • 6,451
  • 5
  • 31
  • 53

1 Answers1

0

Answering your first question is kind of difficult and fairly involved, so I'm going to dive into your subquestion first:

How do I ask a user interface element (e.g. a button) how large it wants to be?

When you create a UI element, you tell it how big it should be (as well as where it should be) via its initWithFrame: constructor; or you can set its frame later via its setFrame: method. It will then draw itself into that space. You can get an element's frame via its frame method.

With that in mind, a BoxLayout class would, hypothetically, be a controller of some sort in which you could add UI elements, and then the BoxLayout controller would arrange them in a grid (or whatever) on an NSView of some sort.

I know you weren't looking for answers that questions motives, but given the complexity of a BoxLayout class vs. laying out the interface in IB, it seems relevant to ask why you want to do this.

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • Well, I don't think working with Interface Builder is that easy, it just happens to be much easier than working with code, because working with code is terrible. If I want to create just a view with 2 Buttons in it, I think it's a bit overkill to create a nib-file and a custom controller class just to handle two buttons. It would be nice to be able to create a window with two buttons in a method (`window=[Window new]; button = [Button new]; [window add: button]; [button setTarget: self selector:@selector(doit:)]; [window show]`) or something like that. – Michael Jun 07 '11 at 21:09
  • And I really don't want to call -initWithFrame:. The Button should know on its own how large it will be. It would feel bad to open IB for each button that I created in code, write down the dimensions, and then put the dimensions in the code. – Michael Jun 07 '11 at 21:11
  • I mean, most controls have a natural size. Next to the comment window in which I write this code, there is an "Add Comment" button. Do you think the HTML-code tells it what its dimensions are? Exactly how many pixels wide it should be? No, Firefox can figure it out and gives it the space that it needs. Why can't Cocoa do the same for me? – Michael Jun 07 '11 at 21:15
  • @Michael: I'm sure there's a default size if you call, e.g, `-[NSButton init]`, and if you set a title you can use `sizeToFit` to automatically size it to fit the label text. – mipadi Jun 07 '11 at 21:18
  • (at least, interface builder has figured out a way to size buttons appropriately. I can make it larger or smaller manually, but out of the box they have just the right side. My questions isn't particularily about buttons though. Thats just an example.) END_OF_COMMENT_LIST – Michael Jun 07 '11 at 21:19
  • @Michael: But truth be told, the Cocoa UI control hierarchy is designed to work hand-in-hand with Interface Builder. You can design UIs programmatically, but then you're working against the system and can expect to have to do some work on your own. (And, to be honest, I think IB is really one of the crown jewels of the OS X development toolchain.) – mipadi Jun 07 '11 at 21:20
  • @mipadi: thanks! I will try that. :-) I just checked the docs... The NSControl class defines -sizeToFit and -calcSize. Maybe that's exactly what I need. – Michael Jun 07 '11 at 21:25
  • maybe you're right. But inside me there is also intellectual curiousity; I just like to know how things work. – Michael Jun 07 '11 at 21:27
  • @Michael: There's not a lot of magic to Interface Builder. It creates an XML file with some values that ultimately get read and used to call methods like `initWithFrame:` with those values. The controls supplied in the IB library are preset with some default values and options. – mipadi Jun 07 '11 at 21:43
  • I just tested it and -sizeToFit works! `NSButton *button = [NSButton new]; [button setTitle:@"YEAH"]; [button sizeToFit]; NSRect bbounds = [button bounds]; NSRect sbounds = [self bounds];` and then I can use `-setFrame` and `-addSubview` to e.g. center the button in the view. – Michael Jun 08 '11 at 11:07