Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa-Touch: How to do layouting

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?

like image 444
Prody Avatar asked Jan 24 '23 01:01

Prody


2 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. You can then call setNeedsLayout to do the layout.

like image 131
Ole Begemann Avatar answered Jan 31 '23 01:01

Ole Begemann


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.

like image 25
Darren Avatar answered Jan 31 '23 02:01

Darren