aarti Garg
aarti Garg

Reputation: 161

How to change the image on UIScrollView

I have a scroll view with multiple images. The images are scrollable.

I am using the following code:

scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

scrollView.pagingEnabled = YES;

scrollView.delegate = self;

scrollView.showsVerticalScrollIndicator = NO;

scrollView.showsHorizontalScrollIndicator = NO;


NSInteger numberOfViews = [arrayImage count];

for (int i = 0; i < numberOfViews; i++)
{
    CGFloat yOrigin = i * scrollView.frame.size.width;
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(yOrigin  +30, 0, self.view.frame.size.width-60 , self.view.frame.size.height)];
    imageView.image = [UIImage imageNamed:[arrayImage objectAtIndex:i]];
    [scrollView addSubview:imageView];
    [imageView release];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberOfViews,0);
[self.view addSubview:scrollView];
[scrollView release]

The application will work with iPad orientations, but the images are not properly scrolled in the landscape mode.

It works fine with portrait mode, but the images merge in landscape mode.

If somebody has any idea, please share....

Upvotes: 0

Views: 1037

Answers (2)

Dushyant Singh
Dushyant Singh

Reputation: 721

You have to set the image frame for that purpose everytime the interface orientation changes or you have to redraw the images again after the change in orientation

Upvotes: 0

Krunal
Krunal

Reputation: 1328

Try this one...

scrollView.delegate = self;
scrollView.scrollEnabled = YES;

int scrollWidth = 120;
scrollView.contentSize = CGSizeMake(scrollWidth,80);     


int xOffset = 0;
imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];

for(int index=0; index < [imagesName count]; index++)
{       
    UIImageView *img = [[UIImageView alloc] init];
    img.bounds = CGRectMake(10, 10, 50, 50);
    img.frame = CGRectMake(5+xOffset, 0, 50, 50);
    NSLog(@"image: %@",[imagesName objectAtIndex:index]);
    img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
    [images insertObject:img atIndex:index];         
    scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110); 
    [scrollView addSubview:[images objectAtIndex:index]];

    xOffset += 70;
}

Set this...

imagesName = [[NSArray alloc]initWithObjects:@"image1.jpg",@"image2.jpg",@"image3.jpg",@"image4.jpg",@"image5.jpg",@"image6.png",@"image7.png",@"image9.png",nil];
images = [[NSMutableArray alloc]init];

Upvotes: 1

Related Questions