Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ahan.tm

macrumors regular
Original poster
Jun 26, 2011
141
0
Florida
Hi,

I am trying to display multiple images in a UIScrollView using the following code:

Code:
    [self.view addSubview:jarView];

    NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
    for (int i=0; i<10; ++i) {
        UIImageView *imgView = [[UIImageView alloc] init];
            
                
                imgView.image = [UIImage imageNamed:@"Aqua_Transparent"];
       
                
        imgView.frame = CGRectMake(160, 380, 25, 25);
        [self.jarView addSubview:imgView];
        [imagesArray addObject:imgView];
        

        [imgView release];
    }

The problem is, it does not work. . . Am I doing something wrong?

Thanks for all your responses:):)
 
Hi,

I am trying to display multiple images in a UIScrollView using the following code:

Code:
    [self.view addSubview:jarView];

    NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
    for (int i=0; i<10; ++i) {
        UIImageView *imgView = [[UIImageView alloc] init];
            
                
                imgView.image = [UIImage imageNamed:@"Aqua_Transparent"];
       
                
        imgView.frame = CGRectMake(160, 380, 25, 25);
        [self.jarView addSubview:imgView];
        [imagesArray addObject:imgView];
        

        [imgView release];
    }

The problem is, it does not work. . . Am I doing something wrong?

Thanks for all your responses:):)

So, what is the output of your code? Is it display 1 image only or display nothing?
 
I define it in Interface Builder.

ok. So, you connect it using IBOutlet right? If yes, I think you no need have this line of code "[self.view addSubview:jarView];". But this is not the root cause of your problem. I think you may no define you scroll view correctly.
 
ok. So, you connect it using IBOutlet right? If yes, I think you no need have this line of code "[self.view addSubview:jarView];". But this is not the root cause of your problem. I think you may no define you scroll view correctly.

I already have that line of code. Is there any specific way to define a scroll view?

I have:
Code:
    IBOutlet UIScrollView *jarView;

Code:
@property(nonatomic, retain) IBOutlet UIScrollView *jarView;

Code:
@synthesize jarView;
 
I already have that line of code. Is there any specific way to define a scroll view?

I have:
Code:
    IBOutlet UIScrollView *jarView;

Code:
@property(nonatomic, retain) IBOutlet UIScrollView *jarView;

Code:
@synthesize jarView;

Do you connect the jar view with the scroll view in interface builder?

----------

Do you connect the jar view with the scroll view in interface builder?

or you try code below which is define scroll view programmatically.

Code:
//Define a scroll view.
    UIScrollView * jarView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
    
    //Set the initial coordinate-y of your image.
    float img_coordinate_y = 380;
    
    for (int i=0; i<10; ++i) {
        
        UIImageView *imgView = [[UIImageView alloc] init];
        
        imgView.image = [UIImage imageNamed:@"Aqua_Transparent"];
                
        imgView.frame = CGRectMake(160, img_coordinate_y, 25, 25);
        [jarView addSubview:imgView];
        [imagesArray addObject:imgView];
        
        [imgView release];
        
        //You must dynamically set the coordinate-y of your image since you need display multiple images in 1 scroll view. If not, it just show only one image at one fixed position.
        img_coordinate_y = img_coordinate_y + imgView.frame.size.height + 20;
    }
    
    [self.view addSubview:jarView];
    
    //You must set the needed content size of scroll view so you can scroll the scroll view.
    jarView.contentSize = CGSizeMake(320, img_coordinate_y);

This code should be work. Hope you get it. :)
 
Do you connect the jar view with the scroll view in interface builder?

----------



or you try code below which is define scroll view programmatically.

Code:
//Define a scroll view.
    UIScrollView * jarView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
    
    //Set the initial coordinate-y of your image.
    float img_coordinate_y = 380;
    
    for (int i=0; i<10; ++i) {
        
        UIImageView *imgView = [[UIImageView alloc] init];
        
        imgView.image = [UIImage imageNamed:@"Aqua_Transparent"];
                
        imgView.frame = CGRectMake(160, img_coordinate_y, 25, 25);
        [jarView addSubview:imgView];
        [imagesArray addObject:imgView];
        
        [imgView release];
        
        //You must dynamically set the coordinate-y of your image since you need display multiple images in 1 scroll view. If not, it just show only one image at one fixed position.
        img_coordinate_y = img_coordinate_y + imgView.frame.size.height + 20;
    }
    
    [self.view addSubview:jarView];
    
    //You must set the needed content size of scroll view so you can scroll the scroll view.
    jarView.contentSize = CGSizeMake(320, img_coordinate_y);

This code should be work. Hope you get it. :)

It does not work. . . Also, The image code is in a loop, so there are multiple images, not just one.

Thanks For Your Post:)
 
It does not work. . . Also, The image code is in a loop, so there are multiple images, not just one.

Thanks For Your Post:)

Welcome. :) Can I know what image format you used? Because the above code is worked in my project.
 
The images are .png(s).

ok. It should be work because it is work in my project ... or can you try copy paste the code I gave you into a new view application project (try copy paste into ViewDidLoad method). :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.