I can add several scrollviews with its background colours changed and it works fine. So basically for every view you have different background colours. However when I try to add separate views for each of these, it adds it right on top of the other and I don’t know why.
UIViewController *view1 = [self.storyboard instantiateViewControllerWithIdentifier:@"View1"];
UIViewController *view2 = [self.storyboard instantiateViewControllerWithIdentifier:@"View2"];
UIViewController *view3 = [self.storyboard instantiateViewControllerWithIdentifier:@"View3"];
for (NSInteger i=0; i<ScrollViewControllerNumberOfPages; i++) {
CGFloat yOrigin = i * self.view.frame.size.height;
CGRect subViewFrame = CGRectMake(yOrigin, 0, pageHeight, pageWidth);
NSLog(@"Printing Width and Height %f %f",pageWidth,pageHeight);
UIView *subView = [[UIView alloc] initWithFrame:subViewFrame];
// Pick a random colour for the view
CGFloat randomRed = ((CGFloat)(arc4random() % 1000))/1000;
CGFloat randomGreen = ((CGFloat)(arc4random() % 1000))/1000;
CGFloat randomBlue = ((CGFloat)(arc4random() % 1000))/1000;
subView.backgroundColor = [UIColor colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1];
NSLog(@"printin i %i", i);
if (i == 0)
subView = view1.view;
else if (i == 1)
subView = view2.view;
[self.scrollView addSubview:subView];
}
This is kind of a shot in the dark because your code would need some serious rethinking.
What you’re doing is you allocate a
subview, you modify it’s frame and background and then completely disregard this and set it to a view of previously instantiatedviewcontoller.This should work (in a way):
It might look a bit better if you do it like: