cyclingIsBetter
cyclingIsBetter

Reputation: 17591

IOS: add some sub-scrollview in a scrollview, image don't appear

I have this code:

- (void)viewDidLoad
{ 
[super viewDidLoad];

NSArray *image = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.png"], [UIImage imageNamed:@"2.png"], [UIImage imageNamed:@"3.png"], nil];

for (int i = 0; i < image.count; i++) {
    CGRect frame;
    frame.origin.x = scrollV.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = scrollV.frame.size;

    UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:frame];
    UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];

    [subview setImage:[image objectAtIndex:i]];
    [subScrollView addSubview:subview];
    [scrollV addSubview:subScrollView];
    [subview release];
    [subScrollView release];
}

scrollV.contentSize = CGSizeMake(scrollV.frame.size.width * image.count, scrollV.frame.size.height);

}

scrollV is the main scrollview and it have "paging enabled" if I use this code I have my scrollV with paging enabled but I see inside it only "1.png" at first page, I don't see others png, why?

Upvotes: 1

Views: 2163

Answers (1)

mattjgalloway
mattjgalloway

Reputation: 34912

You're settings the frame of subview to the incorrect value. That is making it the same frame as its subScrollView so it's pushing it off to the right. Try this:

- (void)viewDidLoad
{ 
[super viewDidLoad];

NSArray *image = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.png"], [UIImage imageNamed:@"2.png"], [UIImage imageNamed:@"3.png"], nil];

for (int i = 0; i < image.count; i++) {
    CGRect frame;
    frame.origin.x = scrollV.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = scrollV.frame.size;

    UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:frame];
    UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height)];

    UIImage *image = [image objectAtIndex:i];
    subScrollView.contentSize = image.size;

    [subview setImage:image];
    [subScrollView addSubview:subview];
    [scrollV addSubview:subScrollView];
    [subview release];
    [subScrollView release];
}

scrollV.contentSize = CGSizeMake(scrollV.frame.size.width * image.count, scrollV.frame.size.height);

}

Edit: Also probably best to set the content size of the inner scrollview. I added code to that effect above.

Upvotes: 1

Related Questions