开发者

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?


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜