如何在嵌套的UIScrollview中添加图像

时间:2013-11-21 09:26:14

标签: ios iphone ios5 ios6 uiscrollview

我需要多次在滚动视图中添加图像。我创建了一个scrollview,但图片没有正确附加在那里。

我的代码在这里:

-(void)sampleScroll
{
    int x = 10;
    int y = 20;

    mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    mainScrollView.contentSize = CGSizeMake(50, (y + 95) * 5);

    // further configure
   [self.view addSubview: mainScrollView];
    images = [[NSMutableArray alloc] initWithObjects:@"image0.jpg",@"image1.jpg",@"image2.jpg",@"image3.jpg", nil];


    for(int i=0; i<[images count]; i++)
    {
        NSLog(@"%@",images);
        UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 250, 150)];
        scrollview.showsVerticalScrollIndicator=YES;
        scrollview.scrollEnabled=YES;
        scrollview.userInteractionEnabled=YES;
        scrollview.backgroundColor = [UIColor whiteColor];
        NSString *img = [images objectAtIndex:i];
         NSLog(@"%@",img);
        imageView.image = [UIImage imageNamed:@"image3.jpg"];
        NSLog(@"%@",imageView.image);
        scrollview.contentSize = CGSizeMake(1250,250);
        [scrollview addSubview:imageView];
        [mainScrollView addSubview:scrollview];
        y=y+155;
        //[self myscrollView];
    }
}

请给我一个解决方案。提前谢谢..

1 个答案:

答案 0 :(得分:0)

需要进行帧调整,

int x = 10;
int y = 10;

UIScrollView * mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
mainScrollView.contentSize = CGSizeMake(mainScrollView.frame.size.width, mainScrollView.frame.size.height * COUNTS);
mainScrollView.pagingEnabled = YES;

for(int i = 0; i < COUNTS; i++)
{
   UIScrollView * scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(x, y, mainScrollView.frame.size.width - 20.0, mainScrollView.frame.size.height - 20.0)];
   scrollview.showsVerticalScrollIndicator = YES;
   scrollview.scrollEnabled = YES;
   scrollview.userInteractionEnabled = YES;
   scrollview.backgroundColor = [UIColor whiteColor];

   UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(x,10.0, mainScrollView.frame.size.width - 40.0, mainScrollView.frame.size.height - 40.0)];
   imageView.image = [UIImage imageNamed:@"owl.jpg"];

   [scrollview addSubview:imageView];
   [mainScrollView addSubview:scrollview];

   y += mainScrollView.frame.size.height;
}

    [self.view addSubview:mainScrollView];