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 UILabel
s 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?
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With