Friday, July 26, 2013

Dynamic loading of UIScrollview with two columns in iOS- Xcode

Here I made a scroll view with two columns and loading it dynamically with the help of an array: 


In this there are two columns that are loaded with different arrays of images and labels. Providing a great interface.

// This would generate a great scroll view so that all the small and big size images will get handled automatically.

// not even worry about the size of scroll view it would automatically increase as perthe size of images and array. 

This is Scroll View Generation 

  img=[[NSArray alloc]initWithObjects:@"image name",@"image name", nil];

    img1=[[NSArray alloc]initWithObjects:@""image name,@"image name", nil];
  
    lb=[[NSArray alloc]initWithObjects:@"blue",@"green", nil];
    lb1=[[NSArray alloc]initWithObjects:@"black",@"red", nil];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(5, 112,self.view.frame.size.width ,self.view.frame.size.height)];
    
    
    CGFloat currentY1 = 0.0f;
    CGFloat currentY2 =0.0f;
    
    CGFloat lbCurrentY=0.0f;
    
    CGFloat h1;
    CGFloat h2;
    
    for (int i=0; i<[img count]; i++)
    {
        
        // create image no. 1
    
        NSString *imageName = [img objectAtIndex:i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        
        
        // put image on correct position
        CGRect rect = imageView.frame;
        CGFloat width=152;
        CGFloat height=imageView.frame.size.height/3.0;
        rect = CGRectMake(0, currentY1, width, height);
        imageView.frame = rect;
        
        // update currentY
        
        currentY1 += imageView.frame.size.height+5;
        h1=imageView.frame.size.height;
        
        //Puting image on Button 1
        UIButton *btnimage1=[UIButton buttonWithType:UIButtonTypeCustom];
        btnimage1.frame=rect;
        [btnimage1 setImage:imageView.image forState:UIControlStateNormal];
        btnimage1.tag=[[id1 objectAtIndex:i]integerValue];
        NSLog(@"%d",btnimage1.tag);
        
    
     // setting label of the first image
        
        NSString *lbname1=[lb objectAtIndex:i];
        UILabel *label;
        CGFloat lbWidth=width;
        CGFloat lbHeight=30;
        lbCurrentY=h1-30;
         
        label=[[UILabel alloc]initWithFrame:CGRectMake(0, lbCurrentY, lbWidth, lbHeight)];
        label.text=lbname1;
        label.textColor=[UIColor whiteColor];
        label.backgroundColor=[UIColor blackColor];
        label.alpha=0.4;
        [btnimage1 addSubview:label];
        [scrollView addSubview:btnimage1];
        
    }

   // ----------------------------------------------------------------------------
        
        
        //Create the image No. 2
    
    for(int i=0;i<[img1 count];i++)
    {
        CGFloat lbCurrentY2=0.0f// setting the label Y axis
        
        // making image 2
        
        NSString *imageNamed2=[img1 objectAtIndex:i];
        UIImage *img2=[UIImage imageNamed:imageNamed2];
        UIImageView *imageView2=[[UIImageView alloc]initWithImage:img2];
        
        // frame size of image 2
        
        CGRect rect2=imageView2.frame;
        CGFloat width2=152;
        CGFloat height2=imageView2.frame.size.height/3.0;
        rect2=CGRectMake(width2+3,currentY2, width2,height2);
        imageView2.frame=rect2;
        
        currentY2+=imageView2.frame.size.height+5;
        h2=imageView2.frame.size.height;
        
        // fixing button woith image
        
        UIButton *btnImage2=[UIButton buttonWithType:UIButtonTypeCustom];
        btnImage2.frame=rect2;
        [btnImage2 setImage:imageView2.image forState:UIControlStateNormal];

        // making label 2
        
        NSString *labelName=[lb1 objectAtIndex:i];
        UILabel *label1;
        CGFloat lbwidth2=width2;
        CGFloat lbHeight=30;
        lbCurrentY2=height2-30;
        label1=[[UILabel alloc]initWithFrame:CGRectMake(0,lbCurrentY2,lbwidth2, lbHeight)];
        label1.text=labelName;
        label1.textColor=[UIColor whiteColor];
        label1.backgroundColor=[UIColor blackColor];
        label1.alpha=0.4;
        [btnImage2 addSubview:label1];
        [scrollView addSubview:btnImage2];
    }
        

        scrollView.contentSize = CGSizeMake(151*2,currentY1+h1);
    
    
    
   scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
   [self.view addSubview:scrollView];