iCreative
iCreative

Reputation: 1506

Multiple copy of view in scrollview

I have 2 views with some Lables & textfields in nib files. I want to make multiple copy of the view and add in a scrollview. When i start adding the view in the scrollview, it is added at the top of the scrollview. Help would be appreciated. Thanks in advance.

Upvotes: 0

Views: 195

Answers (3)

Padavan
Padavan

Reputation: 1074

Maybe you need these methods?

-sendSubviewToBack:
–bringSubviewToFront:

Upvotes: 1

Ruben Marin
Ruben Marin

Reputation: 1637

If you want the views to be added one after another, you must calculate the height of your view and modify the frame of each new view being added to the scroll. If, for example, your view is 100px height, then you must add it like this:

// Number of views added to the scroll
NSUInteger numberOfViews = 0;

// View loaded from XIB
theNewView.frame = CGRectMake(theNewView.frame.origin.x, 
                              theNewView.frame.origin.y + numberOfViews*theNewView.frame.size.height,
                              theNewView.frame.size.width,
                              theNewView.frame.size.height);


// Add the view to the scroll and increment the number of views
[scrollView addSubview:theNewView];
numberOfViews++;

Upvotes: 1

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73638

You need to set frame of the views you are adding to the scrollView.

subview.frame = CGRectMake(0, 0, 200, 200);
[self addSubView:c];

CGRectMake defines where to add the subview to the view.

CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height );

Doing this will prevent the views being added in scrollView to be on top of each other.

Upvotes: 3

Related Questions