KevinM
KevinM

Reputation: 1807

Add the same UIView in another UIView multiple times

Is it possible to add the same view multiple times to another view. I'm trying to do this with addSubview, but the end result is the view I've added is only at the last position that was set.

while (count <= [_testItemArray count]) {
    [self.parentView addSubview:self.childrenView];
    yPosition = count * 37;
    [self.childrenView setFrame:CGRectMake(0, yPosition, 0, 0)];
    count++;
}

The app I am building retrieves the information that goes in the childrenView at runtime. The number of childrenView could vary each time.

Upvotes: 2

Views: 2025

Answers (1)

PengOne
PengOne

Reputation: 48406

You can only add an instance of a UIView once, but if you subclass UIView and create a class for your custom view, you can instantiate it as many times as you like.

My suggestion is to make a class MyCustomView that is a subclass of UIView. Then your loop will look like this:

while (count <= [_testItemArray count]) {
    MyCustomView *customView = [[MyCustomView alloc] init];
    [self.parentView addSubview:customView;
    [customView release];
    yPosition = count * 37;
    [customView setFrame:CGRectMake(0, yPosition, 0, 0)];
    count++;
}

Upvotes: 4

Related Questions