Add multiple UIImageview to UIScrollview programmatically

UIView *view = nil;

NSArray *subviews = [scrollView subviews];

CGFloat curXLoc = 0;

for (view in subviews)
{
    if ([view isKindOfClass:[UIView class]] && view.tag > 0)
    {
        CGRect frame = view.frame;

        frame.origin = CGPointMake(curXLoc, 0);

        view.frame = frame;


        curXLoc += (kScrollObjWidth);
    }
}

      

I placed one Scrollview

, in this scroll, I placed 5 UIImageview

in the XIB file

I have one image and one button, whenever I clicked the button, the image must be loaded in everything UIImageview

inside UIScrollview

programmatically.

and also i used the following coding

for(UIImageView *addview in Scroll)
{

    if([addview isKindOfClass:[UIImageView class]])

    {

        UIImageView *newImage = (UIImageView *)addview;

        newImage.image = [UIImage imageNamed:@"EarP010.jpg"];

    }
}

      

+3


source to share


3 answers


Use this



NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"your xib name" owner:nil options:nil];

UIView* mainView = [objects objectAtIndex:0];

for (UIView* view in [mainView subviews]) {
    if([view isKindOfClass:[UIImageView class]])
    {
        UIImageView *iview = (UIImageView *)view;


        iview.image = [UIImage imageNamed:@"your imagename.png"];
    }
}

      

+1


source


Try it,



NSMutableArray * imagesArray = [[NSMutableArray alloc]init];

for (int imageCount = 0; imageCount < (noOfImages);imageCount++)
{
 [imagesArray addObject:[UIImage imageNamed:@"localImagePath%d.png",imageCount]];
}

UIScrollView * scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0,0.0,480.0,960.0)];
scrollview.contentSize = CGSizeMake(scrollview.size.width * (noOfImages),scrollview.frame.size.height);
scrollview.pagingEnabled = YES;

CGFloat xPos = 0.0;

for (UIImage * image in imagesArray) {
@autoreleasepool {
UIImageView * imageview = [[UIImageView alloc]initWithImage:image];
imageview.frame = CGRectMake(xPos, 0.0,scrollview.frame.size.width ,self.scrollView.frame.size.height);
[scrollview addSubview:imageview];
xPos += scrollview.size.width;
}
}

      

+3


source


You can add images to the while loop if you know how many images you need.

     UIScrollView *sv = [UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
     ....
     [self.view addSubView:sv];

     int i = 0;
     while(i < numberOfImages){
         i = i + 200;
         UIImageView *i = [UIImageView alloc] initWithFrame:CGRectMake(20, i, 200, 100)]
         ....
         [sv addSubView:i];
       i++;
     }

      

This is one of the situations you could use. You will need to know the number of image views you want and what Y values ​​are in those images. Most of this code is pseudocode and needs some completion.

+1


source







All Articles