Csabi Csabi - 1 year ago 112
Objective-C Question

Objective C How to Add UiLabel to UiScrollWiev

HI can I add UiLabel programaticaly to UiScrollWiev because I have read
And I did not find any constructor.
Is there Any way for do this?

Answer Source
[myScrollView addSubview:myLabel];
[myLabel release];

Adding a view happens in the parent (container) view, not the view that is being added.

Also once it's added to the superview, the superview owns it and you should release it.

EDIT: OP asks, "But What happens when I create 10 of them are they placed under each other?"

No. Each UIView subclass (which both UIScrollView and UILabel are) have a .frame property, which contains a CGRect struct. That field defines the position and size of the object.


UILabel *one = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
UILabel *two = [[UILabel alloc] initWithFrame:CGRectMake(0, 35, 200, 30)];
UILabel *three = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, 200, 30)];

[myScrollView addSubview:one];
[myScrollView addSubview:two];
[myScrollView addSubview:three];
[one release];
[two release];
[three release];

That'll put three UILabels, each of them 200px wide by 30px high on the scroll view, all of them flush against the left side of that view, one right at the top, one 35 px down, one 70 px down.

If the contents of the UIScrollView are bigger than the .frame of the UIScrollView, you set a .contentSize property (again, a CGRect) that defines the area of the contents that you want to scroll.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download