Help With UIScrollView

Discussion in 'iOS Programming' started by ahan.tm, Nov 27, 2011.

  1. ahan.tm macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #1
    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:):)
     
  2. cthesky macrumors member

    Joined:
    Aug 21, 2011
    #2
    So, what is the output of your code? Is it display 1 image only or display nothing?
     
  3. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #3
    It displays nothing . . .
     
  4. cthesky macrumors member

    Joined:
    Aug 21, 2011
    #4
    ok. Can I know how you define your Scroll View (Jar View)?
     
  5. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #5
    I define it in Interface Builder.
     
  6. cthesky macrumors member

    Joined:
    Aug 21, 2011
    #6
    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.
     
  7. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #7
    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;
     
  8. cthesky macrumors member

    Joined:
    Aug 21, 2011
    #8
    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. :)
     
  9. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #9
    It does not work. . . Also, The image code is in a loop, so there are multiple images, not just one.

    Thanks For Your Post:)
     
  10. cthesky macrumors member

    Joined:
    Aug 21, 2011
    #10
    Welcome. :) Can I know what image format you used? Because the above code is worked in my project.
     
  11. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #11
    The images are .png(s).
     
  12. cthesky macrumors member

    Joined:
    Aug 21, 2011
    #12
    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). :)
     

Share This Page