I am adding subviews to my scrollview according to the number of array elements in a for loop.The container view has an additional subview inside.The problem is that only the first container views contains the added subview and the new one does not but the third one also has it.I checked the Frame of the subviews but still does not seem to work.
CGFloat contentOffset = 0.0f;
//add tej views one by one to scrollview
for (NSString *packageType in packageTypesArray)
{
CGRect lvPackageContainerFrame = CGRectMake(contentOffset, 0.0f,scrollView.frame.size.width,scrollView.frame.size.height);
LVPackageContainer *lvPackageContainer = [[LVPackageContainer alloc] initWithFrame:lvPackageContainerFrame];
NSLog(@"Frame %@",NSStringFromCGRect(lvPackageContainerFrame));
lvPackageContainer.packageType = packageType;
[packageContainersArray addObject:lvPackageContainer];
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 264, 314)];
NSLog(@"BackgroundImage %@",backgroundImage);
backgroundImage.backgroundColor = [UIColor redColor];
[backgroundImage setImage:[UIImage imageNamed:@"shadowBackground.png"]];
backgroundImage.center = lvPackageContainer.center;
[lvPackageContainer addSubview:backgroundImage];
[scrollView addSubview:lvPackageContainer];
contentOffset += lvPackageContainerFrame.size.width;
scrollView.contentSize = CGSizeMake(contentOffset, scrollView.frame.size.height);
}
If you add a subview, the coordinate system is start from its parent view
In your case, when you set center for second view, it will become something like {480,240} (Assuming the view is full screen in iphone)
So, the 3rd one subview you can see the is actually the subview of page 2.
Updated:
When you get the center of a view, it actually calculate from the frame values:
Therefore assume your second view is with
CGRectMake(320, 0, 320, 480), the center of it will beSo your subview will be center at
{480,240}of the second view.If you convert it relative to the scrollview, the subview will be at
{(320+480),(0+240)}, which you will see it is on the 3rd view, not the 2nd one