Cocoa -Touch: how to make a layout

I have a view ( UIScrollView ) that loads some data and displays various things from it in different subtitles. So I have about 10 subviews ( UIImageView, UILabel ) and I need to put them programmatically, given their unpredictable content (i.e. different height / width for the UILabel depending on the text property).

From what I read, there is no layout frame for Cocoa -touch.

What is the best way to do this?

From what I can say, I have to put the content in the views and then start calculating the coordinates based on their frames after calling their sizeToFit methods.

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

+4
source share
4 answers

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

+6
source

Cocoa layout is usually done with automatic resizing (using autoresizingMask ). You start by looking at some hard-coded initial sizes, say 200x200; place your subzones on this view and set the auto-size flags accordingly. This view can then be resized to its actual size, as determined by its parent view / window. The process is the same regardless of whether you use Interface Builder or whether you execute it programmatically.

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

If you need a more complex layout, you need to implement it yourself by overriding layoutSubviews .

+3
source

I do not know any automatic layout managers, etc.

So, I think you will need to calculate the necessary positions and sizes and update the frame your subzones manually.

EDIT: I found this question where Brad Larson points to an example layout manager. NTN

+1
source

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

-one
source

All Articles