user1898829
user1898829

Reputation: 3517

How do I prevent images from being stretched in scrollview

AspectFill doesn't work so well with ScrollView.

This is my code

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = self.scrollView.bounds.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageCount= page;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * self.pageCount, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
    [imageView setImageWithURL:[self.urlArray objectAtIndex:self.pageCount] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    [imageView setContentMode:UIViewContentModeRedraw];
    [self.scrollView addSubview:imageView];

}

Upvotes: 1

Views: 613

Answers (3)

Monish Bansal
Monish Bansal

Reputation: 509

Set scrollview contentmode aspectfill.

Upvotes: 0

Ege Akpinar
Ege Akpinar

Reputation: 3286

I believe your scroll view has autoresizesSubviews enabled, disable it and your images will appear (and the default view mode for images should be just fine)

Upvotes: 0

DD_
DD_

Reputation: 7388

Add this:

[scrollView setContentMode:UIViewContentModeScaleAspectFit];
[imageView sizeToFit];

Should work :-)

Upvotes: 2

Related Questions