2012-01-09 65 views
1

我有这样的代码:IOS:在滚动视图添加一些子滚动视图,图像不会出现

- (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主要滚动视图,它有“启用分页” 如果我使用此代码我有我的scrollV与分页启用,但我看到里面只有“1.png”在第一页,我没有看到其他人PNG,为什么?

回答

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

} 

编辑:也许最好设置内部滚动视图的内容大小。我在上面添加了代码。