2

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.

1 Answer 1

4

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++; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick response. I just edited my Question to reflect that I also see the childrenView's frame. Would the code segment that you posted change to [self.customView setFrame:CGRectMake(0, yPosition, 0, 0)]; ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.