views:

163

answers:

4

I have a view (UIScrollView), which loads some data, and displays various things from it in various subviews. So I have approx 10 subviews (UIImageView, UILabel) and I need to place them programatically considering their unpredictable contents (i.e. different height/width for the UILabels depending on the text property).

From what I've read, there is no layout framework for Cocoa-touch.

What is the best way to do this?

From what I can tell, I should put the contents in the views, then start calculating coordinates based on their frames after calling their sizeToFit methods.

This approach is very error-prone. Is there really no other way?

A: 

You can use Interface Builder to create a view and then drag and drop elements into it.

Jeff Kelley
try reading the question before answering, YES, IB works when I have static content. The question is about what's the best way to do it when the content is dynamic.
Prody
+1  A: 

I'm not aware of any automatic layout managers or the like.

So, I think you'll have to calculate the desired positions and sizes and update the frames of your subviews manually.

EDIT: I found this question where Brad Larson points to an example of a custom layout manager. HTH

Thomas Müller
+4  A: 

You are right, there are no automatic layout managers. Subclassing UIScrollView and overriding layoutSubviews is probably the right way to implement your custom algorithm. You can then call setNeedsLayout to do the layout.

Ole Begemann
+2  A: 

Layout in Cocoa is typically done with auto-resizing (using autoresizingMask). You start with your view at some hard-coded initial size, say 200x200; place your subviews onto this view and set the autoresizing flags accordingly. This view is then free to be resized to its actual size, as determined by its parent view/window. The process is the same whether you use Interface Builder or whether you do it programmatically.

If you need a vertical stack of views you can use a table view.

If you want more complicated layout you need to implement it yourself, by overriding layoutSubviews.

Darren