2012-01-09 67 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); 

} 

編輯:也許最好設置內部滾動視圖的內容大小。我在上面添加了代碼。