IOS:在scrollview中添加一些子scrollview,图像不会出现

时间:2012-01-09 11:16:55

标签: ios xcode uiscrollview

我有这段代码:

- (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是主要的scrollview,它有“分页启用” 如果我使用这个代码,我的scrollV启用了分页,但我在第一页看到里面只有“1.png”,我没有看到其他的png,为什么?

1 个答案:

答案 0 :(得分:1)

您将subview的框架设置为不正确的值。这使得它与subScrollView的框架相同,所以它将它推向右边。试试这个:

- (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);

}

编辑:也可能最好设置内部scrollview的内容大小。我在上面添加了代码。