views:

52

answers:

1

How do I make a resolution-independent iPhone view controller that can stack it's subviews like the browser stacks HTML elements?

Here's an example. One UILabel at the top that could contain 1-6 lines of text; two UIButtons in a row right below the label; the left button could have a short or long title. I want the buttons to appear at the right vertical position where the text ends; I want the left button to scale accordingly to the length of it's title and the right button to reposition automatically.

In other words, I want the most natural behavior for this kind of UI when it's content changes.

Is there a way to tell Interface Builder or UIKit to implement this kind of behavior automatically?

+1  A: 

Cocoa's layout managers are not available on the iPhone. The only automatic layout tool isUIView's autoresizingMask property, which is not enough to solve your problem. So have to code it yourself.

The right place to start is to overwrite the view's layoutSubviews method. It gets called automatically whenever the bounds of the view change. Some controls also have a decent autoresizing-to-content behavior calling their sizeToFit method.

Ole Begemann